package com.mutual.reins.interf.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.mutual.base.exception.BusinessException;
import com.mutual.reins.danger.entity.*;
import com.mutual.reins.danger.service.*;
import com.mutual.reins.utils.constraint.ReinsConst;
import com.mutual.reins.interf.entity.*;
import com.mutual.reins.danger.entity.PrppDangerCoinsSumInFo;
import com.mutual.reins.interf.service.*;
import com.mutual.reins.utils.constraint.UserExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import com.mutual.reins.utils.*;

/**
 * @author zjl
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class GeneratePrppApplyDangerUnitServiceImpl implements IGeneratePrppApplyDangerUnitService {
    @Autowired
    private IReinsInterFlogService reinsInterFlogService;
    @Autowired
    private IReinsEndorUnitService reinsEndorUnitService;
    @Autowired
    private IReinsEndorItemService reinsEndorItemService;
    @Autowired
    private IReinsEndorCededService reinsEndorCededService;
    @Autowired
    private IReinsEndorCoinsService reinsEndorCoinsService;
    @Autowired
    private IReinsEndorEngageService reinsEndorEngageService;
    @Autowired
    private IReinsEndorFeeService reinsEndorFeeService;
    @Autowired
    private IReinsEndorLimitService reinsEndorLimitService;
    @Autowired
    private IReinsPolicyCoinsService iReinsPolicyCoinsService;
    @Autowired
    private IPrppDangerUnitService iPrppDangerUnitService;
    @Autowired
    private IPrppDangerCoinsService iPrppDangerCoinsService;
    @Autowired
    private IPrppDangerItemService iPrppDangerItemService;
    @Autowired
    private IPrppDangerPlanService iPrppDangerPlanService;
    @Autowired
    private IPrppDangerTotService iPrppDangerTotService;
    @Autowired
    private IPrpcDangerUnitService iPrpcDangerUnitService;
    @Autowired
    private IPrpcDangerItemService iPrpcDangerItemService;
    @Autowired
    private IPrpcDangerCoinsService iPrpcDangerCoinsService;
    @Autowired
    private IPrpcDangerPlanService iPrpcDangerPlanService;
    @Autowired
    private IPrpcDangerTotService iPrpcDangerTotService;
    @Autowired
    private IFeoEnquiryService iFeoEnquiryService;
    @Autowired
    private IFeoCoinsService iFeoCoinsService;
    @Autowired
    private IFeoEngageService iFeoEngageService;
    @Autowired
    private IFeoItemService iFeoItemService;
    @Autowired
    private IFeoReinsReceiveService iFeoReinsReceiveService;
    @Autowired
    private IFeoReinsVerifyService iFeoReinsVerifyService;
    @Autowired
    private IFeoTotService iFeoTotService;
    @Autowired
    private IReinsPolicyUnitService iReinsPolicyUnitService;
    @Override
    public ReinsStatusDto generateReinsEndorseInfo(ReinsEndorInterfDto reinsEndorInterfDto) {
        Date inDate = new Date();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");
        log.info("批单提交核批调出时间：" + sim.format(inDate));
        ReinsEndorUnit reinsEndorseUnitDto = reinsEndorInterfDto.getReinsEndorUnitDto();
        ReinsStatusDto reinsStatusDto = new ReinsStatusDto();
        try {
            // 初始化返回信息
            reinsStatusDto = this.generateReinsStatus(reinsEndorInterfDto.getCerTiType(), reinsEndorInterfDto.getCerTiNo());
            // 初始化再保日志表数据
            ReinsInterFlog reinsInterfLogDto = new ReinsInterFlog();
            reinsInterfLogDto.setCerTiNo(reinsEndorInterfDto.getCerTiNo());
            reinsInterfLogDto.setCerTiType(reinsEndorInterfDto.getCerTiType());
            reinsInterfLogDto.setCaseType(reinsEndorInterfDto.getCerTiType());
            reinsInterfLogDto.setProposalNo(reinsEndorseUnitDto.getProposalNo());
            reinsInterfLogDto.setPolicyNo(reinsEndorseUnitDto.getPolicyNo());
            reinsInterfLogDto.setOperatorCode(reinsEndorseUnitDto.getCreaterCode());
            reinsInterfLogDto.setOperatorDate(reinsEndorseUnitDto.getCreateDate());
            reinsInterfLogDto.setOperatorName(reinsEndorseUnitDto.getUnderWritEname());
            // 初始化日志表时默认为不通过状态
            reinsInterfLogDto.setRecordFlag("0");
            // 再保生成中间表，危险单位，日志表
            this.saveAndEndorDangerUnit(reinsEndorInterfDto, reinsInterfLogDto);
        } catch (BusinessException businessException) {
            reinsStatusDto.setStatus("N");
            throw new BusinessException(UserExceptionEnum.REINS_INTERF_MESSAGE.getCode(), UserExceptionEnum.REINS_INTERF_MESSAGE.getMessage());
        }
        Date outDate = new Date();
        log.info("批单提交核批调出时间：" + sim.format(outDate));
        return reinsStatusDto;
    }

    /**
     * 保存接口表信息
     *
     * @param reinsEndorInterfDto 接口入参对象
     * @param reinsInterfLogDto   批单日志信息对象
     */
    public void saveAndEndorDangerUnit(ReinsEndorInterfDto reinsEndorInterfDto, ReinsInterFlog reinsInterfLogDto) {
        //查询序号
        QueryWrapper<ReinsInterFlog> qw = new QueryWrapper<>();
        qw.eq("CERTINO", reinsEndorInterfDto.getCerTiNo());
        qw.orderByDesc("SERIALNO");
        Collection<ReinsInterFlog> reinsInterfLogList = reinsInterFlogService.list(qw);
        if (ObjectUtil.isNotNull(reinsInterfLogList) && reinsInterfLogList.size() > 0) {
            int serialNo = (reinsInterfLogList.iterator().next()).getSerialNo() + 1;
            reinsInterfLogDto.setSerialNo(serialNo);
        } else {
            reinsInterfLogDto.setSerialNo(1);
        }
        //插入记录
        reinsInterFlogService.save(reinsInterfLogDto);
        //在插入中间表数据之前要先删除中间表数据
        this.deleteReinsEndorInterf(reinsEndorInterfDto.getCerTiNo());
        // 存储中间表数据
        this.saveReinsEndorInterf(reinsEndorInterfDto);
        // 存储危险单位数据
        this.saveDangerUnit(reinsEndorInterfDto);
    }

    /**
     * 批单危险单位初始信息生成
     *
     * @param reinsEndorInterfDto 批单接口表对象
     */
    public void saveDangerUnit(ReinsEndorInterfDto reinsEndorInterfDto) {
        ReinsEndorUnit reinsEndorUnit;

        reinsEndorUnit = reinsEndorInterfDto.getReinsEndorUnitDto();
        //校验是否第一次批改
        boolean isFirstEndorse = this.isFirstEndorse(reinsEndorUnit.getEndorNo(), reinsEndorUnit.getPolicyNo());
        //非第一次批改定义上一次批单号
        String preEndorseNo = "";
        if (!isFirstEndorse) {
            // 最近一次批单号
            preEndorseNo = this.getPreviousEndorseNo(reinsEndorUnit.getEndorNo(), reinsEndorUnit.getPolicyNo());
        }
        //生成批改前原始危险单位信息
        Collection<PrppDangerDto> pDangerDtoList = this.genDangerUnitInfo(reinsEndorUnit, isFirstEndorse, preEndorseNo);
        //批改后的标的信息
        Collection<PrppDangerItem> newPrppDangerItemDtoList = this.getNewPrppDangerDtoItemList(reinsEndorInterfDto, isFirstEndorse, preEndorseNo);
        //二次处理item信息
        this.handlePrppDangerItemList(pDangerDtoList, newPrppDangerItemDtoList);
        //生成危险单位TOT信息
        this.genPrppDangerTotInfo(reinsEndorInterfDto, pDangerDtoList, isFirstEndorse, preEndorseNo);
        // 从共保tot表需要反算金额
        this.inversePrppTotInfo(pDangerDtoList,reinsEndorUnit);
        //更新变化保额
        this.updateChgAmount(reinsEndorInterfDto, pDangerDtoList, isFirstEndorse, preEndorseNo);
        //CoinsFlag  0--独家承保 1--主共  2--从共  3--从联
        String coinsFlag = "0";
        if (!coinsFlag.equals(reinsEndorUnit.getCoinsFlag())) {
            this.genPrppDangerCoinsInfo(reinsEndorInterfDto, pDangerDtoList);
        }
        //生成危险单位分期
        this.genPrppDangerPlanInfo(reinsEndorInterfDto, pDangerDtoList);
        // 先删除原有的数据
        this.deletePrppDanger(reinsEndorInterfDto);
        // 保存新生成的数据
        this.saveAllPrppDanger(pDangerDtoList);
        //复制原保单的询价单信息
        copyFeoEnquiryToEndorse(pDangerDtoList, reinsEndorUnit, isFirstEndorse, preEndorseNo);
    }

    /**
     * 保存最新批单危险单位信息
     *
     * @param prppDangerDtoList 批单危险单位对象
     */
    private void saveAllPrppDanger(Collection<PrppDangerDto> prppDangerDtoList) {
        if(ObjectUtil.isNotNull(prppDangerDtoList) && prppDangerDtoList.size()>0){
            for (PrppDangerDto prppDangerDto : prppDangerDtoList) {
                iPrppDangerUnitService.save(prppDangerDto.getPrppDangerUnitDto());
                iPrppDangerItemService.saveBatch(prppDangerDto.getPrppDangerItemDtoList());
                iPrppDangerCoinsService.saveBatch(prppDangerDto.getPrppDangerCoinsDtoList());
                iPrppDangerTotService.saveBatch(prppDangerDto.getPrppDangerTotDtoList());
                iPrppDangerPlanService.saveBatch(prppDangerDto.getPrppDangerPlanDtoList());
            }
        }
    }

    /**
     * 保存前删除批单危险单位信息
     *
     * @param reinsEndorInterfDto 批单接口表对象
     */
    private void deletePrppDanger(ReinsEndorInterfDto reinsEndorInterfDto) {
        ReinsEndorUnit reinsEndorUnit = reinsEndorInterfDto.getReinsEndorUnitDto();
        String endorseNo = reinsEndorUnit.getEndorNo();
        Map<String, Object> columnMap = new HashMap<String, Object>(16) {{
            put("ENDORSENO", endorseNo);
        }};
        iPrppDangerItemService.removeByMap(columnMap);
        iPrppDangerCoinsService.removeByMap(columnMap);
        iPrppDangerPlanService.removeByMap(columnMap);
        iPrppDangerTotService.removeByMap(columnMap);
        iPrppDangerUnitService.removeByMap(columnMap);
    }

    /**
     * 生成危险单位分期信息
     *
     * @param reinsEndorInterfDto 批单接口表对象
     * @param pDangerDtoList      批单危险单位对象集合
     */
    private void genPrppDangerPlanInfo(ReinsEndorInterfDto reinsEndorInterfDto, Collection<PrppDangerDto> pDangerDtoList) {
        Collection<ReinsEndorPlan> reinsEndorPlanList = reinsEndorInterfDto.getReinsEndorPlanDto();
        BigDecimal sumPlanFee = new BigDecimal(0);
        String endorseNo = "";
        for (ReinsEndorPlan reinsEndorPlanDto : reinsEndorPlanList) {
            sumPlanFee = ReinsUtils.add(sumPlanFee, reinsEndorPlanDto.getPlanFee());
            endorseNo = reinsEndorPlanDto.getEndorNo();
        }
        for (PrppDangerDto prppDangerDto : pDangerDtoList) {
            PrppDangerUnit prppDangerUnitDto = prppDangerDto.getPrppDangerUnitDto();
            Collection<PrppDangerPlan> prppDangerPlanDtoList = new ArrayList<>();
            this.genEndorPlanList(prppDangerPlanDtoList, endorseNo, reinsEndorPlanList, prppDangerUnitDto, sumPlanFee);
            BigDecimal premOfVatSum = new BigDecimal(0);
            BigDecimal vatSum = new BigDecimal(0);
            BigDecimal premFreeVatSum = new BigDecimal(0);
            BigDecimal freeVatSum = new BigDecimal(0);
            BigDecimal chgPremOfVatSum = new BigDecimal(0);
            BigDecimal chgVatSum = new BigDecimal(0);
            BigDecimal chgPremFreeVatSum = new BigDecimal(0);
            BigDecimal chgFreeVatSum = new BigDecimal(0);
            PrppDangerPlan prppDangerPlanDto = new PrppDangerPlan();
            BigDecimal planFeeSum = new BigDecimal(0);
            BigDecimal chgPlanFeeSum = new BigDecimal(0);
            for (PrppDangerPlan prppDangerPlan : prppDangerPlanDtoList) {
                prppDangerPlanDto = prppDangerPlan;
                planFeeSum = ReinsUtils.addRound(planFeeSum, prppDangerPlanDto.getPlanFee(), 2);
                chgPlanFeeSum = ReinsUtils.addRound(chgPlanFeeSum, prppDangerPlanDto.getChgPlanFee(), 2);
                premOfVatSum = ReinsUtils.addRound(premOfVatSum, prppDangerPlanDto.getPremOfVat(), 2);
                vatSum = ReinsUtils.addRound(vatSum, prppDangerPlanDto.getVat(), 2);
                premFreeVatSum = ReinsUtils.addRound(premFreeVatSum, prppDangerPlanDto.getPremFreeVat(), 2);
                freeVatSum = ReinsUtils.addRound(freeVatSum, prppDangerPlanDto.getFreeVat(), 2);
                chgPremOfVatSum = ReinsUtils.addRound(chgPremOfVatSum, prppDangerPlanDto.getChgPremOfVat(), 2);
                chgVatSum = ReinsUtils.addRound(chgVatSum, prppDangerPlanDto.getChgVat(), 2);
                chgPremFreeVatSum = ReinsUtils.addRound(chgPremFreeVatSum, prppDangerPlanDto.getChgPremFreeVat(), 2);
                chgFreeVatSum = ReinsUtils.addRound(chgFreeVatSum, prppDangerPlanDto.getChgFreeVat(), 2);
            }
            prppDangerPlanDtoList.clear();
            prppDangerPlanDto.setSerialNo(1);
            prppDangerPlanDto.setPayNo(1);
            prppDangerPlanDto.setPlanFee(planFeeSum);
            prppDangerPlanDto.setChgPlanFee(chgPlanFeeSum);
            prppDangerPlanDto.setPremOfVat(premOfVatSum);
            prppDangerPlanDto.setVat(vatSum);
            prppDangerPlanDto.setPremFreeVat(premFreeVatSum);
            prppDangerPlanDto.setFreeVat(freeVatSum);
            prppDangerPlanDto.setChgPremOfVat(chgPremOfVatSum);
            prppDangerPlanDto.setChgVat(chgVatSum);
            prppDangerPlanDto.setChgPremFreeVat(chgPremFreeVatSum);
            prppDangerPlanDto.setChgFreeVat(chgFreeVatSum);
            prppDangerPlanDto.setExamineComCode(prppDangerPlanDto.getExamineComCode());
            prppDangerPlanDto.setExamineUserCode(prppDangerPlanDto.getExamineUserCode());
            prppDangerPlanDtoList.add(prppDangerPlanDto);
            prppDangerDto.setPrppDangerPlanDtoList(prppDangerPlanDtoList);
        }
    }

    /**
     * 生成批单planList
     *
     * @param prppDangerPlanDtoList 批单危险单位缴费计划
     * @param endorseNo             批单号
     * @param reinsEndorPlanList    批单接口表缴费计划
     * @param prppDangerUnitDto     批单危险单位基本信息
     * @param sumPlanFee            缴费金额
     */
    private void genEndorPlanList(Collection<PrppDangerPlan> prppDangerPlanDtoList, String endorseNo,
                                  Collection<ReinsEndorPlan> reinsEndorPlanList, PrppDangerUnit prppDangerUnitDto, BigDecimal sumPlanFee) {
        if (ObjectUtil.isNotNull(reinsEndorPlanList) && reinsEndorPlanList.size()>0) {
            for (Iterator<ReinsEndorPlan> planIter = reinsEndorPlanList.iterator(); planIter.hasNext(); ) {
                ReinsEndorPlan reinsEndorPlanDto = planIter.next();
                PrppDangerPlan prppDangerPlanDto = new PrppDangerPlan();
                prppDangerPlanDto.setEndorseNo(endorseNo);
                prppDangerPlanDto.setDangerNo(prppDangerUnitDto.getDangerNo());
                prppDangerPlanDto.setSerialNo(reinsEndorPlanDto.getSerialNo());
                prppDangerPlanDto.setPayNo(reinsEndorPlanDto.getPayNo());
                prppDangerPlanDto.setPlanDate(reinsEndorPlanDto.getPlanDate());
                prppDangerPlanDto.setCurrency(prppDangerUnitDto.getCurrency());
                prppDangerPlanDto.setFlag("I");
                this.genPrppDangerPlanChgInFo(planIter, prppDangerPlanDto, prppDangerUnitDto, sumPlanFee, reinsEndorPlanDto);
                prppDangerPlanDtoList.add(prppDangerPlanDto);
            }
        }
    }

    private void genPrppDangerPlanChgInFo(Iterator<ReinsEndorPlan> planIter, PrppDangerPlan prppDangerPlanDto, PrppDangerUnit prppDangerUnitDto, BigDecimal sumPlanFee, ReinsEndorPlan reinsEndorPlanDto) {
        BigDecimal sumDangerChgPlan = new BigDecimal(0);
        BigDecimal sumDangerPlanFee = new BigDecimal(0);
        BigDecimal sumDangerPremOfVat = new BigDecimal(0);
        BigDecimal sumDangerVat = new BigDecimal(0);
        BigDecimal sumDangerPremFreeVat = new BigDecimal(0);
        BigDecimal sumDangerFreeVat = new BigDecimal(0);
        BigDecimal sumDangerChgPremOfVat = new BigDecimal(0);
        BigDecimal sumDangerChgVat = new BigDecimal(0);
        BigDecimal sumDangerChgPremFreeVat = new BigDecimal(0);
        BigDecimal sumDangerChgFreeVat = new BigDecimal(0);
        BigDecimal chgPlanFee = new BigDecimal(0);
        BigDecimal planFee = new BigDecimal(0);
        BigDecimal premOfVat = new BigDecimal(0);
        BigDecimal vat = new BigDecimal(0);
        BigDecimal premFreeVat = new BigDecimal(0);
        BigDecimal freeVat = new BigDecimal(0);
        BigDecimal chgPremOfVat = new BigDecimal(0);
        BigDecimal chgVat = new BigDecimal(0);
        BigDecimal chgPremFreeVat = new BigDecimal(0);
        BigDecimal chgFreeVat = new BigDecimal(0);
        if (planIter.hasNext()) {
            if (sumPlanFee.doubleValue() != 0) {
                chgPlanFee = ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getChgPremium(), reinsEndorPlanDto.getPlanFee()), sumPlanFee, 2);
                planFee = ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getPremium(), reinsEndorPlanDto.getPlanFee()), sumPlanFee, 2);
                premOfVat = ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getPremOfVat(), reinsEndorPlanDto.getPlanFee()), sumPlanFee, 2);
                vat = ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getVat(), reinsEndorPlanDto.getPlanFee()), sumPlanFee, 2);
                premFreeVat = ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getPremFreeVat(), reinsEndorPlanDto.getPlanFee()), sumPlanFee, 2);
                freeVat = ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getFreeVat(), reinsEndorPlanDto.getPlanFee()), sumPlanFee, 2);
                chgPremOfVat = ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getChgPremOfVat(), reinsEndorPlanDto.getPlanFee()), sumPlanFee, 2);
                chgVat = ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getChgVat(), reinsEndorPlanDto.getPlanFee()), sumPlanFee, 2);
                chgPremFreeVat = ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getChgPremFreeVat(), reinsEndorPlanDto.getPlanFee()), sumPlanFee, 2);
                chgFreeVat = ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getChgFreeVat(), reinsEndorPlanDto.getPlanFee()), sumPlanFee, 2);
            }
        } else {
            chgPlanFee = ReinsUtils.subRound(prppDangerUnitDto.getChgPremium(), sumDangerChgPlan, 2);
            planFee = ReinsUtils.subRound(prppDangerUnitDto.getPremium(), sumDangerPlanFee, 2);
            premOfVat = ReinsUtils.subRound(prppDangerUnitDto.getPremOfVat(), sumDangerPremOfVat, 2);
            vat = ReinsUtils.subRound(prppDangerUnitDto.getVat(), sumDangerVat, 2);
            premFreeVat = ReinsUtils.subRound(prppDangerUnitDto.getPremFreeVat(), sumDangerPremFreeVat, 2);
            freeVat = ReinsUtils.subRound(prppDangerUnitDto.getFreeVat(), sumDangerFreeVat, 2);
            chgPremOfVat = ReinsUtils.subRound(prppDangerUnitDto.getChgPremOfVat(), sumDangerChgPremOfVat, 2);
            chgVat = ReinsUtils.subRound(prppDangerUnitDto.getChgVat(), sumDangerChgVat, 2);
            chgPremFreeVat = ReinsUtils.subRound(prppDangerUnitDto.getChgPremFreeVat(), sumDangerChgPremFreeVat, 2);
            chgFreeVat = ReinsUtils.subRound(prppDangerUnitDto.getChgFreeVat(), sumDangerChgFreeVat, 2);
        }
        prppDangerPlanDto.setPlanFee(planFee);
        prppDangerPlanDto.setChgPlanFee(chgPlanFee);
        prppDangerPlanDto.setPremOfVat(premOfVat);
        prppDangerPlanDto.setVat(vat);
        prppDangerPlanDto.setPremFreeVat(premFreeVat);
        prppDangerPlanDto.setFreeVat(freeVat);
        prppDangerPlanDto.setChgPremOfVat(chgPremOfVat);
        prppDangerPlanDto.setChgVat(chgVat);
        prppDangerPlanDto.setChgPremFreeVat(chgPremFreeVat);
        prppDangerPlanDto.setChgFreeVat(chgFreeVat);
        prppDangerPlanDto.setExamineComCode(reinsEndorPlanDto.getExamineComCode());
        prppDangerPlanDto.setExamineUserCode(reinsEndorPlanDto.getExamineUserCode());
    }


    /**
     * 生成共保信息
     *
     * @param reinsEndorInterfDto 批单接口表对象
     * @param pDangerDtoList      批单危险单位对象
     */
    private void genPrppDangerCoinsInfo(ReinsEndorInterfDto reinsEndorInterfDto, Collection<PrppDangerDto> pDangerDtoList) {
        ReinsEndorUnit reinsEndorUnit = reinsEndorInterfDto.getReinsEndorUnitDto();
        QueryWrapper<ReinsPolicyCoins> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("POLICYNO", reinsEndorUnit.getPolicyNo());
        queryWrapper.orderByAsc("SERIALNO");
        List<ReinsPolicyCoins> reinsPolicyCoinsList = iReinsPolicyCoinsService.list(queryWrapper);
        List<ReinsEndorCoins> reinsEndorCoinsList = reinsEndorInterfDto.getReinsEndorCoinsDto();
        ReinsEndorCoins reinsEndorCoinsDto = new ReinsEndorCoins();
        Map<String, ReinsEndorCoins> reinsEndorCoinsDtoMap = Maps.newHashMapWithExpectedSize(0x10);
        PrppDangerCoinsSumInFo prppDangerCoinsSumInFo = new PrppDangerCoinsSumInFo();
        for (ReinsEndorCoins reinsEndorCoins : reinsEndorCoinsList) {
            reinsEndorCoinsDto = reinsEndorCoins;
            reinsEndorCoinsDtoMap.put(reinsEndorCoinsDto.getCoinsCode(), reinsEndorCoinsDto);
        }
        Map<String, PrppDangerCoins> sumCoinsChgValueMap = Maps.newHashMapWithExpectedSize(0x10);
        for (Iterator<ReinsPolicyCoins> coinsIter = reinsPolicyCoinsList.iterator(); coinsIter.hasNext(); ) {
            ReinsPolicyCoins reinsPolicyCoinsDto = coinsIter.next();
            String coinsCode = reinsPolicyCoinsDto.getCoinsCode();
            if (reinsEndorCoinsDtoMap.containsKey(coinsCode)) {
                reinsEndorCoinsDto = reinsEndorCoinsDtoMap.get(coinsCode);
                this.genPrppDangerCoinsSumInFo(prppDangerCoinsSumInFo, reinsEndorCoinsDto);
            }
            for (Iterator<PrppDangerDto> dangerIter = pDangerDtoList.iterator(); dangerIter.hasNext(); ) {
                PrppDangerDto prppDangerDto = dangerIter.next();
                PrppDangerUnit prppDangerUnitDto = prppDangerDto.getPrppDangerUnitDto();
                BigDecimal dangerChgAmount = prppDangerUnitDto.getChgAmount();
                BigDecimal dangerChgPremium = prppDangerUnitDto.getChgPremium();
                BigDecimal dangerChgPremOfVat = prppDangerUnitDto.getChgPremOfVat();
                BigDecimal dangerChgVat = prppDangerUnitDto.getChgVat();
                BigDecimal dangerChgPremFreeVat = prppDangerUnitDto.getChgPremFreeVat();
                BigDecimal dangerChgFreeVat = prppDangerUnitDto.getChgFreeVat();
                Collection<PrppDangerCoins> prppDangerCoinsDtoList = prppDangerDto.getPrppDangerCoinsDtoList();
                Map<String, PrppDangerCoins> prppDangerCoinsDtoMap = Maps.newHashMapWithExpectedSize(0x10);
                if (prppDangerCoinsDtoList != null) {
                    for (Object o : prppDangerCoinsDtoList) {
                        PrppDangerCoins prppDangerCoinsDto = (PrppDangerCoins) o;
                        prppDangerCoinsDtoMap.put(prppDangerCoinsDto.getCoinsCode(), prppDangerCoinsDto);
                    }
                }
                PrppDangerCoins prppDangerCoinsDto;
                if (prppDangerCoinsDtoMap.containsKey(coinsCode)) {
                    prppDangerCoinsDto = prppDangerCoinsDtoMap.get(coinsCode);
                } else {
                    prppDangerCoinsDto = this.coinsToPrppDangerCoins(reinsEndorUnit.getEndorNo(), prppDangerUnitDto, reinsPolicyCoinsDto, reinsEndorCoinsDto);
                    prppDangerCoinsDtoMap.put(coinsCode, prppDangerCoinsDto);
                }
                if (coinsIter.hasNext()) {
                    if (dangerIter.hasNext()) {
                        prppDangerCoinsSumInFo.setDangerChgCoinsAmount(ReinsUtils.divRound(ReinsUtils.mul(dangerChgAmount, reinsPolicyCoinsDto.getCoinsRate()), ReinsConst.INIT_RATE, 2));
                        prppDangerCoinsSumInFo.setDangerChgCoinsPremium(ReinsUtils.divRound(ReinsUtils.mul(dangerChgPremium, reinsPolicyCoinsDto.getCoinsRate()), ReinsConst.INIT_RATE, 2));
                        prppDangerCoinsSumInFo.setDangerChgCoinsPremOfVat(ReinsUtils.divRound(ReinsUtils.mul(dangerChgPremOfVat, reinsPolicyCoinsDto.getCoinsRate()), ReinsConst.INIT_RATE, 2));
                        prppDangerCoinsSumInFo.setDangerChgCoinsVat(ReinsUtils.divRound(ReinsUtils.mul(dangerChgVat, reinsPolicyCoinsDto.getCoinsRate()), ReinsConst.INIT_RATE, 2));
                        prppDangerCoinsSumInFo.setDangerChgCoinsPremFreeVat(ReinsUtils.divRound(ReinsUtils.mul(dangerChgPremFreeVat, reinsPolicyCoinsDto.getCoinsRate()), ReinsConst.INIT_RATE, 2));
                        prppDangerCoinsSumInFo.setDangerChgCoinsFreeVat(ReinsUtils.divRound(ReinsUtils.mul(dangerChgFreeVat, reinsPolicyCoinsDto.getCoinsRate()), ReinsConst.INIT_RATE, 2));
                        this.genSumPrppDangerCoinsChgInFo(sumCoinsChgValueMap, prppDangerCoinsSumInFo, coinsCode);
                    } else {
                        this.genSumPrppDangerCoinsNewChgInFo(sumCoinsChgValueMap, prppDangerCoinsSumInFo, coinsCode);
                    }
                    this.genPrppDangerCoinsChgInFo(prppDangerCoinsSumInFo, prppDangerCoinsDto);
                } else {
                    Collection<PrppDangerCoins> tempPrppDangerCoinsDtoList = prppDangerCoinsDtoMap.values();
                    for (PrppDangerCoins tempPrppDangerCoinsDto : tempPrppDangerCoinsDtoList) {
                        prppDangerCoinsSumInFo.setSumDangerChgCoinsAmount(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgCoinsAmount(), tempPrppDangerCoinsDto.getChgCoinsAmount()));
                        prppDangerCoinsSumInFo.setSumDangerChgCoinsPremium(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgCoinsPremium(), tempPrppDangerCoinsDto.getChgCoinsPremium()));
                        prppDangerCoinsSumInFo.setSumDangerChgPremOfVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgPremOfVat(), tempPrppDangerCoinsDto.getChgPremOfVat()));
                        prppDangerCoinsSumInFo.setSumDangerChgVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgVat(), tempPrppDangerCoinsDto.getChgVat()));
                        prppDangerCoinsSumInFo.setSumDangerChgPremFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgPremFreeVat(), tempPrppDangerCoinsDto.getChgPremFreeVat()));
                        prppDangerCoinsSumInFo.setSumDangerChgFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgFreeVat(), tempPrppDangerCoinsDto.getChgFreeVat()));
                    }
                    this.genPrppDangerCoinsNewInFo(prppDangerUnitDto, prppDangerCoinsDto, prppDangerCoinsSumInFo, dangerChgAmount, dangerChgPremium,
                            dangerChgPremOfVat, dangerChgVat, dangerChgPremFreeVat, dangerChgFreeVat);
                }
                prppDangerCoinsDto.setExamineComCode(reinsEndorCoinsDto.getExamineComCode());
                prppDangerCoinsDto.setExamineUserCode(reinsEndorCoinsDto.getExamineUserCode());
                prppDangerDto.setPrppDangerCoinsDtoList(prppDangerCoinsDtoMap.values());
            }
        }
    }

    private void genPrppDangerCoinsChgInFo(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo, PrppDangerCoins prppDangerCoinsDto) {
        prppDangerCoinsDto.setChgCoinsAmount(prppDangerCoinsSumInFo.getDangerChgCoinsAmount());
        prppDangerCoinsDto.setChgCoinsPremium(prppDangerCoinsSumInFo.getDangerChgCoinsPremium());
        prppDangerCoinsDto.setChgPremOfVat(prppDangerCoinsSumInFo.getDangerChgCoinsPremOfVat());
        prppDangerCoinsDto.setChgVat(prppDangerCoinsSumInFo.getDangerChgCoinsVat());
        prppDangerCoinsDto.setChgPremFreeVat(prppDangerCoinsSumInFo.getDangerChgCoinsPremFreeVat());
        prppDangerCoinsDto.setChgFreeVat(prppDangerCoinsSumInFo.getDangerChgCoinsFreeVat());
    }

    private void genSumPrppDangerCoinsNewChgInFo(Map<String, PrppDangerCoins> sumCoinsChgValueMap, PrppDangerCoinsSumInFo prppDangerCoinsSumInFo, String coinsCode) {
        if (sumCoinsChgValueMap.containsKey(coinsCode)) {
            PrppDangerCoins tempPrppDangerCoinsDto = sumCoinsChgValueMap.get(coinsCode);
            prppDangerCoinsSumInFo.setSumChgCoinsAmount(tempPrppDangerCoinsDto.getChgCoinsAmount());
            prppDangerCoinsSumInFo.setSumChgCoinsPremium(tempPrppDangerCoinsDto.getChgCoinsPremium());
            prppDangerCoinsSumInFo.setSumChgCoinsPremOfVat(tempPrppDangerCoinsDto.getChgPremOfVat());
            prppDangerCoinsSumInFo.setSumChgCoinsVat(tempPrppDangerCoinsDto.getChgVat());
            prppDangerCoinsSumInFo.setSumChgCoinsPremFreeVat(tempPrppDangerCoinsDto.getChgPremFreeVat());
            prppDangerCoinsSumInFo.setSumChgCoinsFreeVat(tempPrppDangerCoinsDto.getChgFreeVat());
        }
        prppDangerCoinsSumInFo.setDangerChgCoinsAmount(ReinsUtils.subRound(prppDangerCoinsSumInFo.getChgCoinsAmount(), prppDangerCoinsSumInFo.getSumChgCoinsAmount(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsPremium(ReinsUtils.subRound(prppDangerCoinsSumInFo.getChgCoinsPremium(), prppDangerCoinsSumInFo.getSumChgCoinsPremium(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsPremOfVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getChgPremOfVat(), prppDangerCoinsSumInFo.getSumChgCoinsPremOfVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getChgVat(), prppDangerCoinsSumInFo.getSumChgCoinsVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsPremFreeVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getChgPremFreeVat(), prppDangerCoinsSumInFo.getSumChgCoinsPremFreeVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsFreeVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getChgFreeVat(), prppDangerCoinsSumInFo.getSumChgCoinsFreeVat(), 2));
    }

    private void genSumPrppDangerCoinsChgInFo(Map<String, PrppDangerCoins> sumCoinsChgValueMap, PrppDangerCoinsSumInFo prppDangerCoinsSumInFo, String coinsCode) {
        if (sumCoinsChgValueMap.containsKey(coinsCode)) {
            PrppDangerCoins tempPrppDangerCoinsDto = sumCoinsChgValueMap.get(coinsCode);
            tempPrppDangerCoinsDto.setChgCoinsAmount(ReinsUtils.add(tempPrppDangerCoinsDto.getChgCoinsAmount(), prppDangerCoinsSumInFo.getDangerChgCoinsAmount()));
            tempPrppDangerCoinsDto.setChgCoinsPremium(ReinsUtils.add(tempPrppDangerCoinsDto.getChgCoinsPremium(), prppDangerCoinsSumInFo.getDangerChgCoinsPremium()));
            tempPrppDangerCoinsDto.setChgPremOfVat(ReinsUtils.add(tempPrppDangerCoinsDto.getChgPremOfVat(), prppDangerCoinsSumInFo.getDangerChgCoinsPremOfVat()));
            tempPrppDangerCoinsDto.setChgVat(ReinsUtils.add(tempPrppDangerCoinsDto.getChgVat(), prppDangerCoinsSumInFo.getDangerChgCoinsVat()));
            tempPrppDangerCoinsDto.setChgPremFreeVat(ReinsUtils.add(tempPrppDangerCoinsDto.getChgPremFreeVat(), prppDangerCoinsSumInFo.getDangerChgCoinsPremFreeVat()));
            tempPrppDangerCoinsDto.setChgFreeVat(ReinsUtils.add(tempPrppDangerCoinsDto.getChgFreeVat(), prppDangerCoinsSumInFo.getDangerChgCoinsFreeVat()));
        } else {
            PrppDangerCoins tempPrppDangerCoinsDto = new PrppDangerCoins();
            tempPrppDangerCoinsDto.setChgCoinsAmount(prppDangerCoinsSumInFo.getDangerChgCoinsAmount());
            tempPrppDangerCoinsDto.setChgCoinsPremium(prppDangerCoinsSumInFo.getDangerChgCoinsPremium());
            sumCoinsChgValueMap.put(coinsCode, tempPrppDangerCoinsDto);
            tempPrppDangerCoinsDto.setChgPremOfVat(prppDangerCoinsSumInFo.getDangerChgCoinsPremOfVat());
            tempPrppDangerCoinsDto.setChgVat(prppDangerCoinsSumInFo.getDangerChgCoinsVat());
            tempPrppDangerCoinsDto.setChgPremFreeVat(prppDangerCoinsSumInFo.getDangerChgCoinsPremFreeVat());
            tempPrppDangerCoinsDto.setChgFreeVat(prppDangerCoinsSumInFo.getDangerChgCoinsFreeVat());
        }
    }

    private void genPrppDangerCoinsNewInFo(PrppDangerUnit prppDangerUnitDto, PrppDangerCoins prppDangerCoinsDto, PrppDangerCoinsSumInFo prppDangerCoinsSumInFo, BigDecimal dangerChgAmount, BigDecimal dangerChgPremium, BigDecimal dangerChgPremOfVat, BigDecimal dangerChgVat, BigDecimal dangerChgPremFreeVat, BigDecimal dangerChgFreeVat) {
        prppDangerCoinsSumInFo.setSumDangerChgCoinsAmount(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumDangerChgCoinsAmount(), 2));
        prppDangerCoinsSumInFo.setSumDangerChgCoinsPremium(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumDangerChgCoinsPremium(), 2));
        prppDangerCoinsSumInFo.setSumDangerChgPremOfVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumDangerChgPremOfVat(), 2));
        prppDangerCoinsSumInFo.setSumDangerChgVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumDangerChgVat(), 2));
        prppDangerCoinsSumInFo.setSumDangerChgPremFreeVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumDangerChgPremFreeVat(), 2));
        prppDangerCoinsSumInFo.setSumDangerChgFreeVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumDangerChgFreeVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsAmount(ReinsUtils.subRound(dangerChgAmount, prppDangerCoinsSumInFo.getSumDangerChgCoinsAmount(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsPremium(ReinsUtils.subRound(dangerChgPremium, prppDangerCoinsSumInFo.getSumDangerChgCoinsPremium(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsPremOfVat(ReinsUtils.subRound(dangerChgPremOfVat, prppDangerCoinsSumInFo.getSumDangerChgPremOfVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsVat(ReinsUtils.subRound(dangerChgVat, prppDangerCoinsSumInFo.getSumDangerChgVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsPremFreeVat(ReinsUtils.subRound(dangerChgPremFreeVat, prppDangerCoinsSumInFo.getSumDangerChgPremFreeVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgCoinsFreeVat(ReinsUtils.subRound(dangerChgFreeVat, prppDangerCoinsSumInFo.getSumDangerChgFreeVat(), 2));
        prppDangerCoinsDto.setChgCoinsAmount(prppDangerCoinsSumInFo.getDangerChgCoinsAmount());
        prppDangerCoinsDto.setChgCoinsPremium(prppDangerCoinsSumInFo.getDangerChgCoinsPremium());
        prppDangerCoinsDto.setChgPremOfVat(prppDangerCoinsSumInFo.getDangerChgCoinsPremOfVat());
        prppDangerCoinsDto.setChgVat(prppDangerCoinsSumInFo.getDangerChgCoinsVat());
        prppDangerCoinsDto.setChgPremFreeVat(prppDangerCoinsSumInFo.getDangerChgCoinsPremFreeVat());
        prppDangerCoinsDto.setChgFreeVat(prppDangerCoinsSumInFo.getDangerChgCoinsFreeVat());
        prppDangerCoinsDto.setChgMiddleCostFee(ReinsUtils.divRound(ReinsUtils.mul(prppDangerUnitDto.getChgDisFee(), prppDangerCoinsDto.getCoinsRate()), ReinsConst.INIT_RATE, 2));
    }


    private PrppDangerCoins coinsToPrppDangerCoins(String endorNo, PrppDangerUnit prppDangerUnitDto, ReinsPolicyCoins reinsPolicyCoinsDto, ReinsEndorCoins reinsEndorCoinsDto) {
        PrppDangerCoins prppDangerCoinsDto = new PrppDangerCoins();
        prppDangerCoinsDto.setEndorseNo(endorNo);
        prppDangerCoinsDto.setDangerNo(prppDangerUnitDto.getDangerNo());
        prppDangerCoinsDto.setSerialNo(reinsPolicyCoinsDto.getSerialNo());
        prppDangerCoinsDto.setMainEndorseNo(endorNo);
        prppDangerCoinsDto.setCoinsCode(reinsPolicyCoinsDto.getCoinsCode());
        prppDangerCoinsDto.setCoinsName(reinsPolicyCoinsDto.getCoinsName());
        prppDangerCoinsDto.setCoinsType(reinsPolicyCoinsDto.getCoinsType());
        prppDangerCoinsDto.setCoinsRate(reinsPolicyCoinsDto.getCoinsRate());
        prppDangerCoinsDto.setChiefFlag(reinsPolicyCoinsDto.getChiefFlag());
        prppDangerCoinsDto.setProportionFlag(reinsPolicyCoinsDto.getProportionFlag());
        prppDangerCoinsDto.setExamineComCode(reinsPolicyCoinsDto.getExamineComCode());
        prppDangerCoinsDto.setExamineUserCode(reinsPolicyCoinsDto.getExamineUserCode());
        prppDangerCoinsDto.setCurrency(prppDangerUnitDto.getCurrency());
        prppDangerCoinsDto.setCoinsAmount(ReinsConst.INIT_VALUE);
        prppDangerCoinsDto.setCoinsPremium(ReinsConst.INIT_VALUE);
        prppDangerCoinsDto.setPremOfVat(ReinsConst.INIT_VALUE);
        prppDangerCoinsDto.setVat(ReinsConst.INIT_VALUE);
        prppDangerCoinsDto.setPremFreeVat(ReinsConst.INIT_VALUE);
        prppDangerCoinsDto.setFreeVat(ReinsConst.INIT_VALUE);
        prppDangerCoinsDto.setExamineComCode(reinsEndorCoinsDto.getExamineComCode());
        prppDangerCoinsDto.setExamineUserCode(reinsEndorCoinsDto.getExamineUserCode());

        return prppDangerCoinsDto;
    }

    private void genPrppDangerCoinsSumInFo(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo, ReinsEndorCoins reinsEndorCoinsDto) {
        prppDangerCoinsSumInFo.setChgCoinsAmount(reinsEndorCoinsDto.getChgCoinsAmount());
        prppDangerCoinsSumInFo.setChgCoinsPremium(reinsEndorCoinsDto.getChgCoinsPremium());
        prppDangerCoinsSumInFo.setChgPremOfVat(reinsEndorCoinsDto.getChgPremOfVat());
        prppDangerCoinsSumInFo.setChgVat(reinsEndorCoinsDto.getChgVat());
        prppDangerCoinsSumInFo.setChgPremFreeVat(reinsEndorCoinsDto.getChgPremFreeVat());
        prppDangerCoinsSumInFo.setChgFreeVat(reinsEndorCoinsDto.getChgFreeVat());
    }

    /**
     * 生成危险单位coins共保信息
     *
     * @param reinsEndorInterfDto 批单接口表对象
     * @param pDangerDtoList      批单危险单位对象
     * @param isFirstEndorse      是否第一次批改
     * @param preEndorseNo        上一次批单号
     */
    private void updateChgAmount(ReinsEndorInterfDto reinsEndorInterfDto, Collection<PrppDangerDto> pDangerDtoList, boolean isFirstEndorse, String preEndorseNo) {
        ReinsEndorUnit reinsEndorUnit = reinsEndorInterfDto.getReinsEndorUnitDto();
        Map<String, Object> qwPolicy = new HashMap<String, Object>(16) {{
            put("POLICYNO", reinsEndorUnit.getPolicyNo());
        }};
        Map<String, Object> qwPrppDanger = new HashMap<String, Object>(16) {{
            put("ENDORSENO", preEndorseNo);
        }};
        Collection<ReinsPolicyCoins> reinsPolicyCoinsDtoList = iReinsPolicyCoinsService.listByMap(qwPolicy);
        List<ReinsEndorCoins> reinsEndorCoinsDtoList = reinsEndorInterfDto.getReinsEndorCoinsDto();
        PrppDangerCoinsSumInFo prppDangerCoinsSumInFo = new PrppDangerCoinsSumInFo();
        BigDecimal baseRate = new BigDecimal(0);
        if (reinsEndorUnit.getCoinsFlag().equals(ReinsConst.COINSFLAG_CG)) {
            for (ReinsPolicyCoins reinsPolicyCoinsDto : reinsPolicyCoinsDtoList) {
                if ("1".equals(reinsPolicyCoinsDto.getCoinsType())) {
                    baseRate = ReinsUtils.add(baseRate, reinsPolicyCoinsDto.getCoinsRate());
                }
            }
            if (isFirstEndorse) {
                this.genPrppDangerCoinsListInFo(qwPolicy, prppDangerCoinsSumInFo);
            } else {
                Collection<PrppDangerCoins> prppDangerCoinsDtoList = iPrppDangerCoinsService.listByMap(qwPrppDanger);
                for (PrppDangerCoins prppDangerCoinsDto : prppDangerCoinsDtoList) {
                    prppDangerCoinsSumInFo.setSumCoinsOldAmount(ReinsUtils.add(ReinsUtils.add(prppDangerCoinsSumInFo.getSumCoinsOldAmount(), prppDangerCoinsDto.getCoinsAmount()), prppDangerCoinsDto.getChgCoinsAmount()));
                }
            }
            for (ReinsEndorCoins reinsEndorCoinsDto : reinsEndorCoinsDtoList) {
                this.genEndorCoinsSumInFo(prppDangerCoinsSumInFo, reinsEndorCoinsDto);
            }
            this.genSumCoinsRound(prppDangerCoinsSumInFo);
        }
        List<ReinsEndorFee> reinsEndorFeeList = reinsEndorInterfDto.getReinsEndorFeeDto();
        if (isFirstEndorse) {
            Collection<PrpcDangerUnit> prpcDangerUnitDtoList = iPrpcDangerUnitService.listByMap(qwPolicy);
            for (PrpcDangerUnit prpcDangerUnitDto : prpcDangerUnitDtoList) {
                prppDangerCoinsSumInFo.setSumFeeOldAmount(ReinsUtils.add(prppDangerCoinsSumInFo.getSumFeeOldAmount(), prpcDangerUnitDto.getAmount()));
            }
        } else {
            Collection<PrppDangerUnit> prppDangerUnitDtoList = iPrppDangerUnitService.listByMap(qwPrppDanger);
            for (PrppDangerUnit prppDangerUnitDto : prppDangerUnitDtoList) {
                prppDangerCoinsSumInFo.setSumFeeOldAmount(ReinsUtils.add(ReinsUtils.add(prppDangerCoinsSumInFo.getSumFeeOldAmount(), prppDangerUnitDto.getAmount()), prppDangerUnitDto.getChgAmount()));
            }
        }
        for (ReinsEndorFee reinsEndorFeeDto : reinsEndorFeeList) {
            this.genEndorCoinsSumChgInFo(prppDangerCoinsSumInFo, reinsEndorFeeDto);
        }
        this.genEndorCoinsSumChgInFoRound(prppDangerCoinsSumInFo);
        for (Iterator<PrppDangerDto> dangerIter = pDangerDtoList.iterator(); dangerIter.hasNext(); ) {
            PrppDangerDto prppDangerDto = dangerIter.next();
            PrppDangerUnit prppDangerUnitDto = prppDangerDto.getPrppDangerUnitDto();
            Collection<PrppDangerTot> prppDangerTotDtoList = prppDangerDto.getPrppDangerTotDtoList();
            for (PrppDangerTot prppDangerTotDto : prppDangerTotDtoList) {
                this.genPrppDangerTotSum(prppDangerTotDto, prppDangerCoinsSumInFo);
            }
            this.genPrppDangerTotSumRound(prppDangerCoinsSumInFo);
            if (ReinsConst.COINSFLAG_CG.equals(reinsEndorUnit.getCoinsFlag())) {
                if (dangerIter.hasNext()) {
                    this.genPrppDangerTotChgSum(prppDangerCoinsSumInFo, prppDangerUnitDto, baseRate);
                } else {
                    this.genPrppDangerTotChgSumRound(prppDangerCoinsSumInFo);
                }
            } else {
                if (dangerIter.hasNext()) {
                    BigDecimal dangerAmount = ReinsUtils.add(prppDangerUnitDto.getAmount(), prppDangerCoinsSumInFo.getDangerChgAmount());
                    if (prppDangerCoinsSumInFo.getSumFeeAmount().doubleValue() != 0) {
                        prppDangerCoinsSumInFo.setDangerShare(ReinsUtils.mulRound(ReinsUtils.div(dangerAmount, prppDangerCoinsSumInFo.getSumFeeAmount()), ReinsConst.INIT_RATE, 4));
                    }
                    this.genSumDangerInFo(prppDangerCoinsSumInFo);
                } else {
                    this.genSumDangerChgInFo(prppDangerCoinsSumInFo);
                }
            }
            this.genPrppDangerUnitInFo(reinsEndorUnit, prppDangerCoinsSumInFo, prppDangerUnitDto);
        }
    }

    private void genPrppDangerCoinsListInFo(Map<String, Object> qwPolicy, PrppDangerCoinsSumInFo prppDangerCoinsSumInFo) {
        Collection<PrpcDangerCoins> prpcDangerCoinsDtoList = iPrpcDangerCoinsService.listByMap(qwPolicy);
        for (PrpcDangerCoins prpcDangerCoinsDto : prpcDangerCoinsDtoList) {
            prppDangerCoinsSumInFo.setSumCoinsOldAmount(ReinsUtils.add(prppDangerCoinsSumInFo.getSumCoinsOldAmount(), prpcDangerCoinsDto.getCoinsAmount()));
        }
    }

    private void genPrppDangerUnitInFo(ReinsEndorUnit reinsEndorUnit, PrppDangerCoinsSumInFo prppDangerCoinsSumInFo, PrppDangerUnit prppDangerUnitDto) {
        prppDangerUnitDto.setEndorseTimes(reinsEndorUnit.getEndorseTimes());
        prppDangerUnitDto.setStartDate(reinsEndorUnit.getStartDate());
        prppDangerUnitDto.setEndDate(reinsEndorUnit.getEndDate());
        prppDangerUnitDto.setDangerShare(prppDangerCoinsSumInFo.getDangerShare());
        prppDangerUnitDto.setChgAmount(prppDangerCoinsSumInFo.getDangerChgAmount());
        prppDangerUnitDto.setChgPremium(prppDangerCoinsSumInFo.getDangerChgPremium());
        prppDangerUnitDto.setChgPremOfVat(prppDangerCoinsSumInFo.getDangerChgPremOfVat());
        prppDangerUnitDto.setChgVat(prppDangerCoinsSumInFo.getDangerChgVat());
        prppDangerUnitDto.setChgPremFreeVat(prppDangerCoinsSumInFo.getDangerChgPremFreeVat());
        prppDangerUnitDto.setChgFreeVat(prppDangerCoinsSumInFo.getDangerChgFreeVat());
    }

    private void genSumDangerChgInFo(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo) {
        prppDangerCoinsSumInFo.setDangerChgAmount(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumFeeChgAmount(), prppDangerCoinsSumInFo.getSumDangerChgAmount(), 2));
        prppDangerCoinsSumInFo.setDangerChgPremium(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumFeeChgPremium(), prppDangerCoinsSumInFo.getSumDangerChgPremium(), 2));
        prppDangerCoinsSumInFo.setDangerChgPremOfVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumFeeChgPremOfVat(), prppDangerCoinsSumInFo.getSumDangerChgPremOfVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumFeeChgVat(), prppDangerCoinsSumInFo.getSumDangerChgVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgPremFreeVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumFeeChgPremFreeVat(), prppDangerCoinsSumInFo.getSumDangerChgPremFreeVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgFreeVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumFeeChgFreeVat(), prppDangerCoinsSumInFo.getSumDangerChgFreeVat(), 2));
        prppDangerCoinsSumInFo.setSumDangerShare(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumDangerShare(), 4));
        prppDangerCoinsSumInFo.setDangerShare(ReinsUtils.subRound(ReinsConst.INIT_RATE, prppDangerCoinsSumInFo.getSumDangerShare(), 4));
    }

    private void genSumDangerInFo(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo) {
        prppDangerCoinsSumInFo.setSumDangerShare(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerShare(), prppDangerCoinsSumInFo.getDangerShare()));
        prppDangerCoinsSumInFo.setSumDangerChgAmount(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgAmount(), prppDangerCoinsSumInFo.getDangerChgAmount()));
        prppDangerCoinsSumInFo.setSumDangerChgPremium(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgPremium(), prppDangerCoinsSumInFo.getDangerChgPremium()));
        prppDangerCoinsSumInFo.setSumDangerChgPremOfVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgPremOfVat(), prppDangerCoinsSumInFo.getDangerChgPremOfVat()));
        prppDangerCoinsSumInFo.setSumDangerChgVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgVat(), prppDangerCoinsSumInFo.getDangerChgVat()));
        prppDangerCoinsSumInFo.setSumDangerChgPremFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgPremFreeVat(), prppDangerCoinsSumInFo.getDangerChgPremFreeVat()));
        prppDangerCoinsSumInFo.setSumDangerChgFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgFreeVat(), prppDangerCoinsSumInFo.getDangerChgFreeVat()));
    }

    private void genPrppDangerTotChgSumRound(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo) {
        prppDangerCoinsSumInFo.setDangerChgAmount(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumCoinsChgAmount(), prppDangerCoinsSumInFo.getSumDangerChgAmount(), 2));
        prppDangerCoinsSumInFo.setDangerChgPremium(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumCoinsChgPremium(), prppDangerCoinsSumInFo.getSumDangerChgPremium(), 2));
        prppDangerCoinsSumInFo.setDangerChgPremOfVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumCoinsChgPremOfVat(), prppDangerCoinsSumInFo.getSumDangerChgPremOfVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumCoinsChgVat(), prppDangerCoinsSumInFo.getSumDangerChgVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgPremFreeVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumCoinsChgPremFreeVat(), prppDangerCoinsSumInFo.getSumDangerChgPremFreeVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgFreeVat(ReinsUtils.subRound(prppDangerCoinsSumInFo.getSumCoinsChgFreeVat(), prppDangerCoinsSumInFo.getSumDangerChgFreeVat(), 2));
        prppDangerCoinsSumInFo.setSumDangerShare(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumDangerShare(), 4));
        prppDangerCoinsSumInFo.setDangerShare(ReinsUtils.subRound(ReinsConst.INIT_RATE, prppDangerCoinsSumInFo.getSumDangerShare(), 4));
    }

    private void genPrppDangerTotChgSum(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo, PrppDangerUnit prppDangerUnitDto, BigDecimal baseRate) {
        prppDangerCoinsSumInFo.setDangerChgAmount(ReinsUtils.mulRound(ReinsUtils.div(prppDangerCoinsSumInFo.getDangerChgAmount(), baseRate), ReinsConst.INIT_RATE, 2));
        prppDangerCoinsSumInFo.setDangerChgPremium(ReinsUtils.mulRound(ReinsUtils.div(prppDangerCoinsSumInFo.getDangerChgPremium(), baseRate), ReinsConst.INIT_RATE, 2));
        prppDangerCoinsSumInFo.setDangerChgPremOfVat(ReinsUtils.mulRound(ReinsUtils.div(prppDangerCoinsSumInFo.getDangerChgPremOfVat(), baseRate), ReinsConst.INIT_RATE, 4));
        prppDangerCoinsSumInFo.setDangerChgVat(ReinsUtils.mulRound(ReinsUtils.div(prppDangerCoinsSumInFo.getDangerChgVat(), baseRate), ReinsConst.INIT_RATE, 2));
        prppDangerCoinsSumInFo.setDangerChgPremFreeVat(ReinsUtils.mulRound(ReinsUtils.div(prppDangerCoinsSumInFo.getDangerChgPremFreeVat(), baseRate), ReinsConst.INIT_RATE, 2));
        prppDangerCoinsSumInFo.setDangerChgFreeVat(ReinsUtils.mulRound(ReinsUtils.div(prppDangerCoinsSumInFo.getDangerChgFreeVat(), baseRate), ReinsConst.INIT_RATE, 2));
        prppDangerCoinsSumInFo.setSumDangerChgAmount(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgAmount(), prppDangerCoinsSumInFo.getDangerChgAmount()));
        prppDangerCoinsSumInFo.setSumDangerChgPremium(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgPremium(), prppDangerCoinsSumInFo.getDangerChgPremium()));
        prppDangerCoinsSumInFo.setSumDangerChgPremOfVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgPremOfVat(), prppDangerCoinsSumInFo.getDangerChgPremOfVat()));
        prppDangerCoinsSumInFo.setSumDangerChgVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgVat(), prppDangerCoinsSumInFo.getDangerChgVat()));
        prppDangerCoinsSumInFo.setSumDangerChgPremFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgPremFreeVat(), prppDangerCoinsSumInFo.getDangerChgPremFreeVat()));
        prppDangerCoinsSumInFo.setSumDangerChgFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerChgFreeVat(), prppDangerCoinsSumInFo.getDangerChgFreeVat()));
        BigDecimal dangerAmount = ReinsUtils.add(prppDangerUnitDto.getAmount(), prppDangerCoinsSumInFo.getDangerChgAmount());
        if (prppDangerCoinsSumInFo.getSumCoinsAmount().doubleValue() != 0) {
            prppDangerCoinsSumInFo.setDangerShare(ReinsUtils.mulRound(ReinsUtils.div(dangerAmount, prppDangerCoinsSumInFo.getSumCoinsAmount()), ReinsConst.INIT_RATE, 4));
        }
        prppDangerCoinsSumInFo.setSumDangerShare(ReinsUtils.add(prppDangerCoinsSumInFo.getSumDangerShare(), prppDangerCoinsSumInFo.getDangerShare()));
    }

    private void genPrppDangerTotSumRound(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo) {
        prppDangerCoinsSumInFo.setDangerChgAmount(ReinsUtils.scale(prppDangerCoinsSumInFo.getDangerChgAmount(), 2));
        prppDangerCoinsSumInFo.setDangerChgPremium(ReinsUtils.scale(prppDangerCoinsSumInFo.getDangerChgPremium(), 2));
        prppDangerCoinsSumInFo.setDangerChgPremOfVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getDangerChgPremOfVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getDangerChgVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgPremFreeVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getDangerChgPremFreeVat(), 2));
        prppDangerCoinsSumInFo.setDangerChgFreeVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getDangerChgFreeVat(), 2));
    }

    private void genPrppDangerTotSum(PrppDangerTot prppDangerTotDto, PrppDangerCoinsSumInFo prppDangerCoinsSumInFo) {
        prppDangerCoinsSumInFo.setDangerChgAmount(ReinsUtils.add(prppDangerCoinsSumInFo.getDangerChgAmount(), prppDangerTotDto.getChgAmountEx()));
        prppDangerCoinsSumInFo.setDangerChgPremium(ReinsUtils.add(prppDangerCoinsSumInFo.getDangerChgPremium(), prppDangerTotDto.getChgPremiumEx()));
        prppDangerCoinsSumInFo.setDangerChgPremOfVat(ReinsUtils.add(prppDangerCoinsSumInFo.getDangerChgPremOfVat(), prppDangerTotDto.getChgPremOfVatEx()));
        prppDangerCoinsSumInFo.setDangerChgVat(ReinsUtils.add(prppDangerCoinsSumInFo.getDangerChgVat(), prppDangerTotDto.getChgVatEx()));
        prppDangerCoinsSumInFo.setDangerChgPremFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getDangerChgPremFreeVat(), prppDangerTotDto.getChgPremFreeVatEx()));
        prppDangerCoinsSumInFo.setDangerChgFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getDangerChgFreeVat(), prppDangerTotDto.getChgFreeVatEx()));
    }

    private void genEndorCoinsSumChgInFoRound(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo) {
        prppDangerCoinsSumInFo.setSumFeeOldAmount(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumFeeOldAmount(), 2));
        prppDangerCoinsSumInFo.setSumFeeChgAmount(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumFeeChgAmount(), 2));
        prppDangerCoinsSumInFo.setSumFeeChgPremium(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumFeeChgPremium(), 2));
        prppDangerCoinsSumInFo.setSumFeeChgPremOfVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumFeeChgPremOfVat(), 2));
        prppDangerCoinsSumInFo.setSumFeeChgVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumFeeChgVat(), 2));
        prppDangerCoinsSumInFo.setSumFeeChgPremFreeVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumFeeChgPremFreeVat(), 2));
        prppDangerCoinsSumInFo.setSumFeeChgFreeVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumFeeChgFreeVat(), 2));
        prppDangerCoinsSumInFo.setSumFeeAmount(ReinsUtils.addRound(prppDangerCoinsSumInFo.getSumFeeOldAmount(), prppDangerCoinsSumInFo.getSumFeeChgAmount(), 2));
    }

    private void genEndorCoinsSumChgInFo(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo, ReinsEndorFee reinsEndorFeeDto) {
        prppDangerCoinsSumInFo.setSumFeeChgAmount(ReinsUtils.add(prppDangerCoinsSumInFo.getSumFeeChgAmount(), reinsEndorFeeDto.getChgAmountEx()));
        prppDangerCoinsSumInFo.setSumFeeChgPremium(ReinsUtils.add(prppDangerCoinsSumInFo.getSumFeeChgPremium(), reinsEndorFeeDto.getChgPremiumEx()));
        prppDangerCoinsSumInFo.setSumFeeChgPremOfVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumFeeChgPremOfVat(), reinsEndorFeeDto.getChgPremOfVatEx()));
        prppDangerCoinsSumInFo.setSumFeeChgVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumFeeChgVat(), reinsEndorFeeDto.getChgVatEx()));
        prppDangerCoinsSumInFo.setSumFeeChgPremFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumFeeChgPremFreeVat(), reinsEndorFeeDto.getChgPremFreeVatEx()));
        prppDangerCoinsSumInFo.setSumFeeChgFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumFeeChgFreeVat(), reinsEndorFeeDto.getChgFreeVatEx()));
    }

    private void genSumCoinsRound(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo) {
        prppDangerCoinsSumInFo.setSumCoinsChgAmount(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumCoinsChgAmount(), 2));
        prppDangerCoinsSumInFo.setSumCoinsChgPremium(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumCoinsChgPremium(), 2));
        prppDangerCoinsSumInFo.setSumCoinsOldAmount(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumCoinsOldAmount(), 2));
        prppDangerCoinsSumInFo.setSumCoinsChgPremOfVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumCoinsChgPremOfVat(), 2));
        prppDangerCoinsSumInFo.setSumCoinsChgVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumCoinsChgVat(), 2));
        prppDangerCoinsSumInFo.setSumCoinsChgPremFreeVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumCoinsChgPremFreeVat(), 2));
        prppDangerCoinsSumInFo.setSumCoinsChgFreeVat(ReinsUtils.scale(prppDangerCoinsSumInFo.getSumCoinsChgFreeVat(), 2));
        prppDangerCoinsSumInFo.setSumCoinsAmount(ReinsUtils.addRound(prppDangerCoinsSumInFo.getSumCoinsOldAmount(), prppDangerCoinsSumInFo.getSumCoinsChgAmount(), 2));
    }

    private void genEndorCoinsSumInFo(PrppDangerCoinsSumInFo prppDangerCoinsSumInFo, ReinsEndorCoins reinsEndorCoinsDto) {
        prppDangerCoinsSumInFo.setSumCoinsChgAmount(ReinsUtils.addRound(prppDangerCoinsSumInFo.getSumCoinsChgAmount(), reinsEndorCoinsDto.getChgCoinsAmount(), 2));
        prppDangerCoinsSumInFo.setSumCoinsChgPremium(ReinsUtils.add(prppDangerCoinsSumInFo.getSumCoinsChgPremium(), reinsEndorCoinsDto.getChgCoinsPremium()));
        prppDangerCoinsSumInFo.setSumCoinsChgPremOfVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumCoinsChgPremOfVat(), reinsEndorCoinsDto.getChgPremOfVat()));
        prppDangerCoinsSumInFo.setSumCoinsChgVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumCoinsChgVat(), reinsEndorCoinsDto.getChgVat()));
        prppDangerCoinsSumInFo.setSumCoinsChgPremFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumCoinsChgPremFreeVat(), reinsEndorCoinsDto.getChgPremFreeVat()));
        prppDangerCoinsSumInFo.setSumCoinsChgFreeVat(ReinsUtils.add(prppDangerCoinsSumInFo.getSumCoinsChgFreeVat(), reinsEndorCoinsDto.getChgFreeVat()));
    }

    /**
     * 反算批单Tot表金额
     *
     * @param pDangerDtoList 批单危险单位
     */
    private void inversePrppTotInfo(Collection<PrppDangerDto> pDangerDtoList,ReinsEndorUnit reinsEndorUnit) {
        QueryWrapper<PrpcDangerCoins> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("POLICYNO", reinsEndorUnit.getPolicyNo());
        List<PrpcDangerCoins> prppDangerCoinsList = iPrpcDangerCoinsService.list(queryWrapper);
        BigDecimal baseRate = ReinsConst.INIT_VALUE;
        for (PrpcDangerCoins prpcDangerCoins: prppDangerCoinsList) {
            if (ReinsConst.COINSTYPE_ME.equals(prpcDangerCoins.getCoinsType()) || ReinsConst.COINSTYPE_OTH_IN.equals(prpcDangerCoins.getCoinsType()))
            {
                baseRate = ReinsUtils.add(baseRate,prpcDangerCoins.getCoinsRate());
            }
            if(baseRate.compareTo(ReinsConst.INIT_RATE)>=0){
                baseRate=ReinsConst.INIT_RATE;
            }
        }
        for (PrppDangerDto pDangerDto : pDangerDtoList) {
            PrppDangerUnit prppDangerUnitDto = pDangerDto.getPrppDangerUnitDto();
            Collection<PrppDangerTot> prppDangerTotDtoList = pDangerDto.getPrppDangerTotDtoList();
            String coinsFlag = prppDangerUnitDto.getCoinsFlag();
            if (ReinsConst.COINSFLAG_CG.equals(coinsFlag)) {
                for (Object o : prppDangerTotDtoList) {
                    PrppDangerTot prppDangerTotDto = (PrppDangerTot) o;
                    prppDangerTotDto.setChgAmount(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getChgAmount(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setChgPremium(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getChgPremium(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setChgAmountEx(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getChgAmountEx(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setChgPremiumEx(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getChgPremiumEx(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setPremOfVat(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getPremOfVat(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setChgPremOfVat(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getChgPremOfVat(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setPremOfVatEx(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getPremOfVatEx(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setChgPremOfVatEx(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getChgPremOfVatEx(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setPremFreeVat(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getPremFreeVat(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setChgPremFreeVat(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getChgPremFreeVat(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setPremFreeVatEx(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getPremFreeVatEx(), ReinsConst.INIT_RATE), baseRate, 2));
                    prppDangerTotDto.setChgPremFreeVatEx(ReinsUtils.divRound(ReinsUtils.mul(prppDangerTotDto.getChgPremFreeVatEx(), ReinsConst.INIT_RATE), baseRate, 2));
                }
            }
        }
    }

    /**
     * 批单tot危险单位信息二次处理
     *
     * @param pDangerDtoList 批单危险单位对象
     * @param isFirstEndorse 是否是第一次批改
     * @param preEndorseNo   上一次批单号
     */
    private void genPrppDangerTotInfo(ReinsEndorInterfDto reinsEndorInterfDto, Collection<PrppDangerDto> pDangerDtoList, boolean isFirstEndorse, String preEndorseNo) {
        PrppDangerTotSumInFo prppDangerTotSumInFo = new PrppDangerTotSumInFo();
        String dangerCurrency = "";
        ReinsEndorUnit reinsEndorUnit = reinsEndorInterfDto.getReinsEndorUnitDto();
        Map<String, ReinsEndorFee> reinsEndorFeeDtoMap = Maps.newHashMapWithExpectedSize(0x10);
        if (!isFirstEndorse) {
            this.genPrppDangerFeeForEndor(reinsEndorFeeDtoMap, preEndorseNo);
        } else {
            this.genPrppDangerFeeForPolicy(reinsEndorFeeDtoMap, reinsEndorUnit.getPolicyNo());
        }
        List<ReinsEndorFee> reinsEndorFeeList = reinsEndorInterfDto.getReinsEndorFeeDto();
        for (ReinsEndorFee reinsEndorFeeDto : reinsEndorFeeList) {
            String sCurrency = reinsEndorFeeDto.getSCurrency();
            reinsEndorFeeDtoMap.put(sCurrency, reinsEndorFeeDto);
        }
        for (String s : reinsEndorFeeDtoMap.keySet()) {
            ReinsEndorFee reinsEndorFeeDto = reinsEndorFeeDtoMap.get(s);
            this.genPrppTotSumInFo(reinsEndorFeeDto, prppDangerTotSumInFo);
            dangerCurrency = reinsEndorFeeDto.getTCurrency();
        }
        this.genPrppDangerFeeSumRound(prppDangerTotSumInFo);
        for (Iterator<PrppDangerDto> dangerIterator = pDangerDtoList.iterator(); dangerIterator.hasNext(); ) {
            PrppDangerDto prppDangerDto = dangerIterator.next();
            PrppDangerUnit prppDangerUnitDto = prppDangerDto.getPrppDangerUnitDto();
            Collection<PrppDangerItem> prppDangerItemDtoList = prppDangerDto.getPrppDangerItemDtoList();
            Collection<PrppDangerTot> prppDangerTotDtoList = prppDangerDto.getPrppDangerTotDtoList();
            Map<String, PrppDangerTot> prppDangerTotDtoMap = Maps.newHashMapWithExpectedSize(0x10);
            if (prppDangerTotDtoList != null) {
                for (PrppDangerTot prppDangerTotDto : prppDangerTotDtoList) {
                    prppDangerTotDtoMap.put(prppDangerTotDto.getSCurrency(), prppDangerTotDto);
                }
            }
            this.genPrppDangerItemChgInFo(prppDangerItemDtoList, prppDangerTotDtoMap, reinsEndorUnit.getEndorNo(), prppDangerUnitDto);
            for (Iterator<String> totIterator = prppDangerTotDtoMap.keySet().iterator(); totIterator.hasNext(); ) {
                String sCurrency = totIterator.next();
                PrppDangerTot prppDangerTotDto = prppDangerTotDtoMap.get(sCurrency);
                ReinsEndorFee reinsEndorFeeDto = reinsEndorFeeDtoMap.get(sCurrency);
                BigDecimal exchRate = reinsEndorFeeDto.getExchRate();
                if (!dangerIterator.hasNext() && !totIterator.hasNext()) {
                    this.genPrppDangerTotSumInFo(prppDangerTotSumInFo);
                } else {
                    this.genPrppDangerTotToSumInFo(prppDangerTotSumInFo, prppDangerTotDto, exchRate);
                }

                prppDangerTotDto.setTCurrency(reinsEndorFeeDto.getTCurrency());
                prppDangerTotDto.setExchRate(exchRate);
                prppDangerTotDto.setChgAmountEx(prppDangerTotSumInFo.getChgAmountEx());
                prppDangerTotDto.setChgPremiumEx(prppDangerTotSumInFo.getChgPremiumEx());
                prppDangerTotDto.setChgPremOfVatEx(prppDangerTotSumInFo.getChgPremOfVatEx());
                prppDangerTotDto.setChgVatEx(prppDangerTotSumInFo.getChgVatEx());
                prppDangerTotDto.setChgPremFreeVatEx(prppDangerTotSumInFo.getChgPremFreeVatEx());
                prppDangerTotDto.setChgFreeVatEx(prppDangerTotSumInFo.getChgFreeVatEx());
                prppDangerTotSumInFo.setSumDangerChgAmountEx(ReinsUtils.add(prppDangerTotSumInFo.getSumDangerChgAmountEx(), prppDangerTotSumInFo.getChgAmountEx()));
                prppDangerTotSumInFo.setSumDangerChgPremiumEx(ReinsUtils.add(prppDangerTotSumInFo.getSumDangerChgPremiumEx(), prppDangerTotSumInFo.getChgPremiumEx()));
                prppDangerTotSumInFo.setSumDangerChgPremOfVatEx(ReinsUtils.add(prppDangerTotSumInFo.getSumDangerChgPremOfVatEx(), prppDangerTotSumInFo.getChgPremOfVatEx()));
                prppDangerTotSumInFo.setSumDangerChgPremFreeVatEx(ReinsUtils.add(prppDangerTotSumInFo.getSumDangerChgPremFreeVatEx(), prppDangerTotSumInFo.getChgPremFreeVatEx()));
                prppDangerTotSumInFo.setSumDangerChgVatEx(ReinsUtils.add(prppDangerTotSumInFo.getSumDangerChgVatEx(), prppDangerTotSumInFo.getChgVatEx()));
                prppDangerTotSumInFo.setSumDangerChgFreeVatEx(ReinsUtils.add(prppDangerTotSumInFo.getSumDangerChgFreeVatEx(), prppDangerTotSumInFo.getChgFreeVatEx()));
            }
            prppDangerUnitDto.setCurrency(dangerCurrency);
            prppDangerUnitDto.setChgAmount(prppDangerTotSumInFo.getSumDangerChgAmountEx());
            prppDangerUnitDto.setChgPremium(prppDangerTotSumInFo.getSumDangerChgPremiumEx());
            prppDangerUnitDto.setChgPremOfVat(prppDangerTotSumInFo.getSumDangerChgPremOfVatEx());
            prppDangerUnitDto.setChgPremFreeVat(prppDangerTotSumInFo.getSumDangerChgPremFreeVatEx());
            prppDangerUnitDto.setChgVat(prppDangerTotSumInFo.getSumDangerChgVatEx());
            prppDangerUnitDto.setChgFreeVat(prppDangerTotSumInFo.getSumDangerChgFreeVatEx());
            BigDecimal sumDangerAmount = ReinsUtils.add(prppDangerUnitDto.getAmount(), prppDangerUnitDto.getChgAmount());
            if (prppDangerTotSumInFo.getSumFeeAmount().doubleValue() != 0) {
                prppDangerUnitDto.setDangerShare(ReinsConst.INIT_VALUE);
            } else {
                prppDangerUnitDto.setDangerShare(ReinsUtils.scale(ReinsUtils.mul(ReinsUtils.div(sumDangerAmount, prppDangerTotSumInFo.getSumFeeAmount()), ReinsConst.INIT_VALUE), 4));
            }
            prppDangerDto.setPrppDangerTotDtoList(prppDangerTotDtoMap.values());
        }
    }
    private void genPrppDangerFeeSumRound(PrppDangerTotSumInFo prppDangerTotSumInFo) {
        prppDangerTotSumInFo.setSumFeeAmount(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeeAmount(), 2));
        prppDangerTotSumInFo.setSumFeePremium (ReinsUtils.scale(prppDangerTotSumInFo.getSumFeePremium(), 2));
        prppDangerTotSumInFo.setSumFeeChgAmount(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeeChgAmount(), 2));
        prppDangerTotSumInFo.setSumFeeChgPremium(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeeChgPremium(), 2));
        prppDangerTotSumInFo.setSumFeeChgPremOfVat(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeeChgPremOfVat(), 2));
        prppDangerTotSumInFo.setSumFeeChgFreeVat(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeeChgFreeVat(), 2));
        prppDangerTotSumInFo.setSumFeeChgPremFreeVat(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeeChgPremFreeVat(), 2));
        prppDangerTotSumInFo.setSumFeeChgVat(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeeChgVat(), 2));
        prppDangerTotSumInFo.setSumFeePremOfVat(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeePremOfVat(), 2));
        prppDangerTotSumInFo.setSumFeeVat(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeeVat(), 2));
        prppDangerTotSumInFo.setSumFeePremFreeVat(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeePremFreeVat(), 2));
        prppDangerTotSumInFo.setSumFeeFreeVat(ReinsUtils.scale(prppDangerTotSumInFo.getSumFeeFreeVat(), 2));
    }

    private void genPrppDangerTotToSumInFo(PrppDangerTotSumInFo prppDangerTotSumInFo, PrppDangerTot prppDangerTotDto, BigDecimal exchRate) {
        prppDangerTotSumInFo.setChgAmountEx(ReinsUtils.mulRound(prppDangerTotDto.getChgAmount(), exchRate, 2));
        prppDangerTotSumInFo.setChgPremiumEx(ReinsUtils.mulRound(prppDangerTotDto.getChgPremium(), exchRate, 2));
        prppDangerTotSumInFo.setChgPremOfVatEx(ReinsUtils.mulRound(prppDangerTotDto.getChgPremOfVat(), exchRate, 2));
        prppDangerTotSumInFo.setChgVatEx(ReinsUtils.mulRound(prppDangerTotDto.getChgVat(), exchRate, 2));
        prppDangerTotSumInFo.setChgPremFreeVatEx(ReinsUtils.mulRound(prppDangerTotDto.getChgPremFreeVat(), exchRate, 2));
        prppDangerTotSumInFo.setChgFreeVatEx(ReinsUtils.mulRound(prppDangerTotDto.getChgFreeVat(), exchRate, 2));
        prppDangerTotSumInFo.setSumChgAmountEx(ReinsUtils.add(prppDangerTotSumInFo.getSumChgAmountEx(), prppDangerTotSumInFo.getChgAmountEx()));
        prppDangerTotSumInFo.setSumChgPremiumEx(ReinsUtils.add(prppDangerTotSumInFo.getSumChgPremiumEx(), prppDangerTotSumInFo.getChgPremiumEx()));
        prppDangerTotSumInFo.setSumChgPremOfVatEx(ReinsUtils.add(prppDangerTotSumInFo.getSumChgPremOfVatEx(), prppDangerTotSumInFo.getChgPremOfVatEx()));
        prppDangerTotSumInFo.setSumChgVatEx(ReinsUtils.add(prppDangerTotSumInFo.getSumChgVatEx(), prppDangerTotSumInFo.getChgVatEx()));
        prppDangerTotSumInFo.setSumChgPremFreeVatEx(ReinsUtils.add(prppDangerTotSumInFo.getSumChgPremFreeVatEx(), prppDangerTotSumInFo.getChgPremFreeVatEx()));
        prppDangerTotSumInFo.setSumChgFreeVatEx(ReinsUtils.add(prppDangerTotSumInFo.getSumChgFreeVatEx(), prppDangerTotSumInFo.getChgFreeVatEx()));

    }

    private void genPrppDangerTotSumInFo(PrppDangerTotSumInFo prppDangerTotSumInFo) {
        prppDangerTotSumInFo.setChgAmountEx(ReinsUtils.subRound(prppDangerTotSumInFo.getSumFeeChgAmount(), prppDangerTotSumInFo.getSumChgAmountEx(), 2));
        prppDangerTotSumInFo.setChgPremiumEx(ReinsUtils.subRound(prppDangerTotSumInFo.getSumFeeChgPremium(), prppDangerTotSumInFo.getSumChgPremiumEx(), 2));
        prppDangerTotSumInFo.setChgPremOfVatEx(ReinsUtils.subRound(prppDangerTotSumInFo.getSumFeeChgPremOfVat(), prppDangerTotSumInFo.getSumChgPremOfVatEx(), 2));
        prppDangerTotSumInFo.setChgPremFreeVatEx(ReinsUtils.subRound(prppDangerTotSumInFo.getSumFeeChgPremFreeVat(), prppDangerTotSumInFo.getSumChgPremFreeVatEx(), 2));
        prppDangerTotSumInFo.setChgVatEx(ReinsUtils.subRound(prppDangerTotSumInFo.getSumFeeChgVat(), prppDangerTotSumInFo.getSumChgVatEx(), 2));
        prppDangerTotSumInFo.setChgFreeVatEx(ReinsUtils.subRound(prppDangerTotSumInFo.getSumFeeChgFreeVat(), prppDangerTotSumInFo.getSumChgFreeVatEx(), 2));

    }

    private void genPrppTotSumInFo(ReinsEndorFee reinsEndorFeeDto, PrppDangerTotSumInFo prppDangerTotSumInFo) {
        prppDangerTotSumInFo.setSumFeeChgAmount(ReinsUtils.add(prppDangerTotSumInFo.getSumFeeChgAmount(), reinsEndorFeeDto.getChgAmountEx()));
        prppDangerTotSumInFo.setSumFeeChgPremium(ReinsUtils.add(prppDangerTotSumInFo.getSumFeeChgPremium(), reinsEndorFeeDto.getChgPremiumEx()));
        prppDangerTotSumInFo.setSumFeeAmount(ReinsUtils.add(ReinsUtils.add(prppDangerTotSumInFo.getSumFeeAmount(), reinsEndorFeeDto.getAmountEx()), reinsEndorFeeDto.getChgAmountEx()));
        prppDangerTotSumInFo.setSumFeePremium(ReinsUtils.add(ReinsUtils.add(prppDangerTotSumInFo.getSumFeePremium(), reinsEndorFeeDto.getPremiumEx()), reinsEndorFeeDto.getChgPremiumEx()));
        prppDangerTotSumInFo.setSumFeeChgPremOfVat(ReinsUtils.add(prppDangerTotSumInFo.getSumFeeChgPremOfVat(), reinsEndorFeeDto.getChgPremOfVatEx()));
        prppDangerTotSumInFo.setSumFeeChgFreeVat(ReinsUtils.add(prppDangerTotSumInFo.getSumFeeChgFreeVat(), reinsEndorFeeDto.getChgFreeVatEx()));
        prppDangerTotSumInFo.setSumFeeChgPremFreeVat(ReinsUtils.add(prppDangerTotSumInFo.getSumFeeChgPremFreeVat(), reinsEndorFeeDto.getChgPremFreeVatEx()));
        prppDangerTotSumInFo.setSumFeeChgVat(ReinsUtils.add(prppDangerTotSumInFo.getSumFeeChgVat(), reinsEndorFeeDto.getChgVatEx()));
        prppDangerTotSumInFo.setSumFeePremOfVat(ReinsUtils.add(ReinsUtils.add(prppDangerTotSumInFo.getSumFeePremOfVat(), reinsEndorFeeDto.getPremOfVaTex()), reinsEndorFeeDto.getChgPremOfVatEx()));
        prppDangerTotSumInFo.setSumFeeVat(ReinsUtils.add(ReinsUtils.add(prppDangerTotSumInFo.getSumFeeVat(), reinsEndorFeeDto.getFreeVatEx()), reinsEndorFeeDto.getChgFreeVatEx()));
        prppDangerTotSumInFo.setSumFeePremFreeVat(ReinsUtils.add(ReinsUtils.add(prppDangerTotSumInFo.getSumFeePremFreeVat(), reinsEndorFeeDto.getPremFreeVatEx()), reinsEndorFeeDto.getChgPremFreeVatEx()));
        prppDangerTotSumInFo.setSumFeeFreeVat(ReinsUtils.add(ReinsUtils.add(prppDangerTotSumInFo.getSumFeeFreeVat(), reinsEndorFeeDto.getFreeVatEx()), reinsEndorFeeDto.getChgFreeVatEx()));
    }

    private void genPrppDangerItemChgInFo(Collection<PrppDangerItem> prppDangerItemDtoList, Map<String, PrppDangerTot> prppDangerTotDtoMap, String endorseNo, PrppDangerUnit prppDangerUnitDto) {
        for (PrppDangerItem prppDangerItemDto : prppDangerItemDtoList) {
            String sCurrency = prppDangerItemDto.getCurrency();
            if (prppDangerTotDtoMap.containsKey(sCurrency)) {
                PrppDangerTot prppDangerTotDto = prppDangerTotDtoMap.get(sCurrency);
                if (ReinsConst.CALCULATE_FALG.equals(prppDangerItemDto.getCalculateFlag())) {
                    prppDangerTotDto.setChgAmount(ReinsUtils.addRound(prppDangerTotDto.getChgAmount(), prppDangerItemDto.getChgAmount(), 2));
                }
                prppDangerTotDto.setChgPremium(ReinsUtils.addRound(prppDangerTotDto.getChgPremium(), prppDangerItemDto.getChgPremium(), 2));
                prppDangerTotDto.setChgPremOfVat(ReinsUtils.addRound(prppDangerTotDto.getChgPremOfVat(), prppDangerItemDto.getChgPremOfVat(), 2));
                prppDangerTotDto.setChgPremFreeVat(ReinsUtils.addRound(prppDangerTotDto.getChgPremFreeVat(), prppDangerItemDto.getChgPremFreeVat(), 2));
                prppDangerTotDto.setChgFreeVat(ReinsUtils.addRound(prppDangerTotDto.getChgFreeVat(), prppDangerItemDto.getChgFreeVat(), 2));
                prppDangerTotDto.setChgVat(ReinsUtils.addRound(prppDangerTotDto.getChgVat(), prppDangerItemDto.getChgVat(), 2));
                prppDangerTotDto.setExamineComCode(prppDangerTotDto.getExamineComCode());
                prppDangerTotDto.setExamineUserCode(prppDangerTotDto.getExamineUserCode());
            } else {
                PrppDangerTot prppDangerTotDto = new PrppDangerTot();
                prppDangerTotDto.setEndorseNo(endorseNo);
                prppDangerTotDto.setDangerNo(prppDangerUnitDto.getDangerNo());
                prppDangerTotDto.setSCurrency(sCurrency);
                prppDangerTotDto.setAmount(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setPremium(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setPremOfVat(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setVat(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setPremFreeVat(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setFreeVat(ReinsConst.INIT_VALUE);
                if (ReinsConst.CALCULATE_FALG.equals(prppDangerItemDto.getCalculateFlag())) {
                    prppDangerTotDto.setChgAmount(prppDangerItemDto.getChgAmount());
                }
                prppDangerTotDto.setChgPremium(prppDangerItemDto.getChgPremium());
                prppDangerTotDto.setChgPremOfVat(prppDangerItemDto.getChgPremOfVat());
                prppDangerTotDto.setChgPremFreeVat(prppDangerItemDto.getChgPremFreeVat());
                prppDangerTotDto.setChgVat(prppDangerItemDto.getChgVat());
                prppDangerTotDto.setChgFreeVat(prppDangerItemDto.getChgFreeVat());
                prppDangerTotDto.setExamineComCode(prppDangerItemDto.getExamineComCode());
                prppDangerTotDto.setExamineUserCode(prppDangerItemDto.getExamineUserCode());
                prppDangerTotDtoMap.put(sCurrency, prppDangerTotDto);
            }
        }
    }

    /**
     * 非第一次批改获取批单信息生成批单fee表信息
     *
     * @param reinsEndorFeeDtoMap 批单接口表金额信息Map
     */
    private void genPrppDangerFeeForEndor(Map<String, ReinsEndorFee> reinsEndorFeeDtoMap, String preEndorseNo) {
        QueryWrapper<PrppDangerTot> qw = new QueryWrapper<>();
        qw.eq("ENDORSENO", preEndorseNo);
        Collection<PrppDangerTot> prppDangerTotDtoList = iPrppDangerTotService.list(qw);
        for (PrppDangerTot prppDangerTotDto : prppDangerTotDtoList) {
            String sCurrency = prppDangerTotDto.getSCurrency();
            if (reinsEndorFeeDtoMap.containsKey(sCurrency)) {
                ReinsEndorFee reinsEndorFeeDto = reinsEndorFeeDtoMap.get(sCurrency);
                reinsEndorFeeDto.setAmountEx(ReinsUtils.add(ReinsUtils.add(reinsEndorFeeDto.getAmountEx(), prppDangerTotDto.getAmountEx()), prppDangerTotDto.getChgAmountEx()));
                reinsEndorFeeDto.setPremiumEx(ReinsUtils.add(ReinsUtils.add(reinsEndorFeeDto.getPremiumEx(), prppDangerTotDto.getPremiumEx()), prppDangerTotDto.getChgPremiumEx()));
                reinsEndorFeeDto.setPremOfVaTex(ReinsUtils.add(ReinsUtils.add(reinsEndorFeeDto.getPremOfVaTex(), prppDangerTotDto.getPremOfVatEx()), prppDangerTotDto.getChgPremOfVatEx()));
                reinsEndorFeeDto.setPremFreeVatEx(ReinsUtils.add(ReinsUtils.add(reinsEndorFeeDto.getPremFreeVatEx(), prppDangerTotDto.getPremFreeVatEx()), prppDangerTotDto.getChgPremFreeVatEx()));
                reinsEndorFeeDto.setFreeVatEx(ReinsUtils.add(ReinsUtils.add(reinsEndorFeeDto.getFreeVatEx(), prppDangerTotDto.getFreeVatEx()), prppDangerTotDto.getChgFreeVatEx()));
                reinsEndorFeeDto.setVatEx(ReinsUtils.add(ReinsUtils.add(reinsEndorFeeDto.getVatEx(), prppDangerTotDto.getVatEx()), prppDangerTotDto.getChgVatEx()));
            } else {
                ReinsEndorFee reinsEndorFeeDto = new ReinsEndorFee();
                reinsEndorFeeDto.setSCurrency(sCurrency);
                reinsEndorFeeDto.setTCurrency(prppDangerTotDto.getTCurrency());
                reinsEndorFeeDto.setExchRate(prppDangerTotDto.getExchRate());
                reinsEndorFeeDto.setAmountEx(ReinsUtils.add(prppDangerTotDto.getAmountEx(), prppDangerTotDto.getChgAmountEx()));
                reinsEndorFeeDto.setPremiumEx(ReinsUtils.add(prppDangerTotDto.getPremiumEx(), prppDangerTotDto.getChgPremiumEx()));
                reinsEndorFeeDto.setChgAmountEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDto.setChgPremiumEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDto.setChgPremOfVatEx(ReinsUtils.add(prppDangerTotDto.getPremOfVatEx(), prppDangerTotDto.getChgPremOfVatEx()));
                reinsEndorFeeDto.setPremFreeVatEx(ReinsUtils.add(prppDangerTotDto.getPremFreeVatEx(), prppDangerTotDto.getChgPremFreeVatEx()));
                reinsEndorFeeDto.setFreeVatEx(ReinsUtils.add(prppDangerTotDto.getFreeVatEx(), prppDangerTotDto.getChgFreeVatEx()));
                reinsEndorFeeDto.setVatEx(ReinsUtils.add(prppDangerTotDto.getVatEx(), prppDangerTotDto.getChgVatEx()));
                reinsEndorFeeDto.setChgPremOfVatEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDto.setChgPremFreeVatEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDto.setChgFreeVatEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDto.setChgVatEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDtoMap.put(sCurrency, reinsEndorFeeDto);
            }
        }
    }

    /**
     * 第一次批改获取保单危险单位生成批单fee表信息
     *
     * @param reinsEndorFeeDtoMap 批单接口表金额信息Map
     */
    private void genPrppDangerFeeForPolicy(Map<String, ReinsEndorFee> reinsEndorFeeDtoMap, String policyNo) {
        QueryWrapper<PrpcDangerTot> qw = new QueryWrapper<>();
        qw.eq("POLICYNO", policyNo);
        List<PrpcDangerTot> prpcDangerTotDtoList = iPrpcDangerTotService.list(qw);
        for (PrpcDangerTot prpcDangerTotDto : prpcDangerTotDtoList) {
            String sCurrency = prpcDangerTotDto.getSCurrency();
            if (reinsEndorFeeDtoMap.containsKey(sCurrency)) {
                ReinsEndorFee reinsEndorFeeDto = reinsEndorFeeDtoMap.get(sCurrency);
                reinsEndorFeeDto.setAmountEx(ReinsUtils.add(reinsEndorFeeDto.getAmountEx(), prpcDangerTotDto.getAmountEx()));
                reinsEndorFeeDto.setPremiumEx(ReinsUtils.add(reinsEndorFeeDto.getPremiumEx(), prpcDangerTotDto.getPremiumEx()));
                reinsEndorFeeDto.setPremOfVaTex(ReinsUtils.add(reinsEndorFeeDto.getPremOfVaTex(), prpcDangerTotDto.getPremOfVatEx()));
                reinsEndorFeeDto.setFreeVatEx(ReinsUtils.add(reinsEndorFeeDto.getFreeVatEx(), prpcDangerTotDto.getFreeVatEx()));
                reinsEndorFeeDto.setVatEx(ReinsUtils.add(reinsEndorFeeDto.getVatEx(), prpcDangerTotDto.getVatEx()));
                reinsEndorFeeDto.setPremFreeVatEx(ReinsUtils.add(reinsEndorFeeDto.getPremFreeVatEx(), prpcDangerTotDto.getPremFreeVatEx()));
            } else {
                ReinsEndorFee reinsEndorFeeDto = new ReinsEndorFee();
                reinsEndorFeeDto.setSCurrency(sCurrency);
                reinsEndorFeeDto.setTCurrency(prpcDangerTotDto.getTCurrency());
                reinsEndorFeeDto.setExchRate(prpcDangerTotDto.getExchRate());
                reinsEndorFeeDto.setAmountEx(prpcDangerTotDto.getAmountEx());
                reinsEndorFeeDto.setPremiumEx(prpcDangerTotDto.getPremiumEx());
                reinsEndorFeeDto.setChgAmountEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDto.setChgPremiumEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDto.setPremOfVaTex(prpcDangerTotDto.getPremOfVatEx());
                reinsEndorFeeDto.setPremFreeVatEx(prpcDangerTotDto.getPremFreeVatEx());
                reinsEndorFeeDto.setFreeVatEx(prpcDangerTotDto.getFreeVatEx());
                reinsEndorFeeDto.setVatEx(prpcDangerTotDto.getVatEx());
                reinsEndorFeeDto.setChgPremOfVatEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDto.setChgPremFreeVatEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDto.setChgFreeVatEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDto.setChgVatEx(ReinsConst.INIT_VALUE);
                reinsEndorFeeDtoMap.put(sCurrency, reinsEndorFeeDto);
            }
        }
    }

    /**
     * 二次处理item信息
     *
     * @param pDangerDtoList 批单危险单位对象
     * @param newPrppDangerItemDtoList 批单危险单位标的信息
     */
    private void handlePrppDangerItemList(Collection<PrppDangerDto> pDangerDtoList, Collection<PrppDangerItem> newPrppDangerItemDtoList) {
        for (PrppDangerItem newPrppDangerItemDto : newPrppDangerItemDtoList) {
            for (PrppDangerDto prppDangerDto : pDangerDtoList) {
                PrppDangerUnit prppDangerUnitDto = prppDangerDto.getPrppDangerUnitDto();
                Collection<PrppDangerItem> prppDangerItemDtoList = prppDangerDto.getPrppDangerItemDtoList();
                if (Objects.equals(prppDangerUnitDto.getEndorseNo(), newPrppDangerItemDto.getEndorseNo())) {
                    boolean flag = false;
                    Collection<Integer> serialList = new ArrayList<>();
                    for (PrppDangerItem prppDangerItemDto : prppDangerItemDtoList) {
                        serialList.add(prppDangerItemDto.getSerialNo());
                    }
                    for (PrppDangerItem prppDangerItemDto : prppDangerItemDtoList) {
                        if (serialList.contains(newPrppDangerItemDto.getSerialNo())) {
                            flag = true;
                            if (prppDangerItemDto.getSerialNo().equals(newPrppDangerItemDto.getSerialNo())) {
                                prppDangerItemDto.setAmount(newPrppDangerItemDto.getAmount());
                                prppDangerItemDto.setPremium(newPrppDangerItemDto.getPremium());
                                prppDangerItemDto.setChgAmount(newPrppDangerItemDto.getChgAmount());
                                prppDangerItemDto.setChgPremium(newPrppDangerItemDto.getChgPremium());
                                prppDangerItemDto.setCalculateFlag(newPrppDangerItemDto.getCalculateFlag());
                                prppDangerItemDto.setQuantity(newPrppDangerItemDto.getQuantity());
                                prppDangerItemDto.setChgPremOfVat(newPrppDangerItemDto.getChgPremOfVat());
                                prppDangerItemDto.setChgVat(newPrppDangerItemDto.getChgVat());
                                prppDangerItemDto.setChgPremFreeVat(newPrppDangerItemDto.getChgPremFreeVat());
                                prppDangerItemDto.setChgFreeVat(newPrppDangerItemDto.getChgFreeVat());
                            }
                        }
                    }
                    if (!flag) {
                        prppDangerDto.getPrppDangerItemDtoList().add(newPrppDangerItemDto);
                    }
                }
            }
            for (PrppDangerDto tempPrppDangerDto : pDangerDtoList) {
                if (tempPrppDangerDto.getPrppDangerUnitDto().getDangerNo() == 1) {
                    newPrppDangerItemDto.setDangerNo(1);
                }
            }
        }
    }

    /**
     * 生成批改后批单ITEM信息
     *
     * @param reinsEndorInterfDto 批单接口表对象
     * @param isFirstEndorse 是否第一次批改
     * @param preEndorseNo 上一次批单
     * @return 危险单位item表
     */
    private Collection<PrppDangerItem> getNewPrppDangerDtoItemList(ReinsEndorInterfDto reinsEndorInterfDto, boolean isFirstEndorse, String preEndorseNo) {
        ReinsEndorUnit reinsEndorUnit = reinsEndorInterfDto.getReinsEndorUnitDto();
        String endorseNo = reinsEndorUnit.getEndorNo();
        List<ReinsEndorItem> reinsEndorItemList = reinsEndorInterfDto.getReinsEndorItemDto();
        //获取批改前标的信息
        Map<Integer, PrppDangerItem> dangerItemDtoMap = Maps.newHashMapWithExpectedSize(0x10);
        if (isFirstEndorse) {
            QueryWrapper<PrpcDangerItem> queryPrpcItem = new QueryWrapper<>();
            queryPrpcItem.eq("POLICYNO", reinsEndorUnit.getPolicyNo());
            queryPrpcItem.orderByAsc("DANGERNO", "SERIALNO");
            Collection<PrpcDangerItem> prpcDangerItemDtoList = iPrpcDangerItemService.list(queryPrpcItem);
            for (PrpcDangerItem prpcDangerItemDto : prpcDangerItemDtoList) {
                if (dangerItemDtoMap.containsKey(prpcDangerItemDto.getSerialNo())) {
                    PrppDangerItem prppDangerItemDto = dangerItemDtoMap.get(prpcDangerItemDto.getSerialNo());
                    prppDangerItemDto.setAmount(ReinsUtils.addRound(prppDangerItemDto.getAmount(), prpcDangerItemDto.getAmount(), 2));
                    prppDangerItemDto.setPremium(ReinsUtils.addRound(prppDangerItemDto.getPremium(), prpcDangerItemDto.getPremium(), 2));
                    prppDangerItemDto.setPremOfVat(ReinsUtils.addRound(prppDangerItemDto.getPremOfVat(), prpcDangerItemDto.getPremOfVat(), 2));
                    prppDangerItemDto.setVat(ReinsUtils.addRound(prppDangerItemDto.getVat(), prpcDangerItemDto.getVat(), 2));
                    prppDangerItemDto.setPremFreeVat(ReinsUtils.addRound(prppDangerItemDto.getPremFreeVat(), prpcDangerItemDto.getPremFreeVat(), 2));
                    prppDangerItemDto.setFreeVat(ReinsUtils.addRound(prppDangerItemDto.getFreeVat(), prpcDangerItemDto.getFreeVat(), 2));
                    prppDangerItemDto.setExamineComCode(prppDangerItemDto.getExamineComCode());
                    prppDangerItemDto.setExamineUserCode(prppDangerItemDto.getExamineUserCode());
                } else {
                    PrppDangerItem prppDangerItemDto = this.copyPrppDangerItem(endorseNo, prpcDangerItemDto);
                    dangerItemDtoMap.put(prppDangerItemDto.getSerialNo(), prppDangerItemDto);
                }
            }
        } else {
            QueryWrapper<PrppDangerItem> queryPrepDanger = new QueryWrapper<>();
            queryPrepDanger.eq("ENDORSENO", preEndorseNo);
            Collection<PrppDangerItem> prepDangerItemDtoList = iPrppDangerItemService.list(queryPrepDanger);
            for (PrppDangerItem prepDangerItemDto : prepDangerItemDtoList) {
                if (dangerItemDtoMap.containsKey(prepDangerItemDto.getSerialNo())) {
                    PrppDangerItem prppDangerItemDto = dangerItemDtoMap.get(prepDangerItemDto.getSerialNo());
                    BigDecimal amount = ReinsUtils.addRound(ReinsUtils.add(prppDangerItemDto.getAmount(), prepDangerItemDto.getAmount()), prepDangerItemDto.getChgAmount(), 2);
                    BigDecimal premium = ReinsUtils.addRound(ReinsUtils.add(prppDangerItemDto.getPremium(), prepDangerItemDto.getPremium()), prepDangerItemDto.getChgPremium(), 2);
                    BigDecimal premOfVat = ReinsUtils.addRound(ReinsUtils.add(prppDangerItemDto.getPremOfVat(), prepDangerItemDto.getPremOfVat()), prepDangerItemDto.getChgPremOfVat(), 2);
                    BigDecimal vat = ReinsUtils.addRound(ReinsUtils.add(prppDangerItemDto.getVat(), prepDangerItemDto.getVat()), prepDangerItemDto.getChgVat(), 2);
                    BigDecimal premFreeVat = ReinsUtils.addRound(ReinsUtils.add(prppDangerItemDto.getPremFreeVat(), prepDangerItemDto.getPremFreeVat()), prepDangerItemDto.getChgPremFreeVat(), 2);
                    BigDecimal freeVat = ReinsUtils.addRound(ReinsUtils.add(prppDangerItemDto.getFreeVat(), prepDangerItemDto.getFreeVat()), prepDangerItemDto.getChgFreeVat(), 2);
                    prppDangerItemDto.setAmount(amount);
                    prppDangerItemDto.setPremium(premium);
                    prppDangerItemDto.setPremOfVat(premOfVat);
                    prppDangerItemDto.setVat(vat);
                    prppDangerItemDto.setPremFreeVat(premFreeVat);
                    prppDangerItemDto.setFreeVat(freeVat);
                } else {
                    PrppDangerItem newPrppDangerItemDto = new PrppDangerItem();
                    BeanUtils.copyProperties(prepDangerItemDto, newPrppDangerItemDto);
                    this.genNewPrppDangerItemInFo(endorseNo, newPrppDangerItemDto, prepDangerItemDto);
                    dangerItemDtoMap.put(newPrppDangerItemDto.getSerialNo(), newPrppDangerItemDto);
                }
            }
        }
        for (ReinsEndorItem reinsEndorItemDto : reinsEndorItemList) {
            PrppDangerItem newPrppDangerItemDto = this.copyNewPrppDangerItem(endorseNo, reinsEndorItemDto);
            dangerItemDtoMap.put(newPrppDangerItemDto.getSerialNo(), newPrppDangerItemDto);
        }
        return dangerItemDtoMap.values();
    }

    private void genNewPrppDangerItemInFo(String endorseNo, PrppDangerItem newPrppDangerItemDto, PrppDangerItem prepDangerItemDto) {
        newPrppDangerItemDto.setEndorseNo(endorseNo);
        newPrppDangerItemDto.setAmount(ReinsUtils.addRound(prepDangerItemDto.getAmount(), prepDangerItemDto.getChgAmount(), 2));
        newPrppDangerItemDto.setPremium(ReinsUtils.addRound(prepDangerItemDto.getPremium(), prepDangerItemDto.getChgPremium(), 2));
        newPrppDangerItemDto.setPremOfVat(ReinsUtils.addRound(prepDangerItemDto.getPremOfVat(), prepDangerItemDto.getChgPremOfVat(), 2));
        newPrppDangerItemDto.setVat(ReinsUtils.addRound(prepDangerItemDto.getVat(), prepDangerItemDto.getChgVat(), 2));
        newPrppDangerItemDto.setPremFreeVat(ReinsUtils.addRound(prepDangerItemDto.getPremFreeVat(), prepDangerItemDto.getChgPremFreeVat(), 2));
        newPrppDangerItemDto.setFreeVat(ReinsUtils.addRound(prepDangerItemDto.getFreeVat(), prepDangerItemDto.getChgFreeVat(), 2));
        newPrppDangerItemDto.setChgAmount(ReinsConst.INIT_VALUE);
        newPrppDangerItemDto.setChgPremium(ReinsConst.INIT_VALUE);
        newPrppDangerItemDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
        newPrppDangerItemDto.setChgVat(ReinsConst.INIT_VALUE);
        newPrppDangerItemDto.setChgPremFreeVat(ReinsConst.INIT_VALUE);
        newPrppDangerItemDto.setChgFreeVat(ReinsConst.INIT_VALUE);
    }

    /**
     * 生成批单危险单位ITEM信息
     *
     * @param endorseNo         批单号
     * @param reinsEndorItemDto 接口表标的信息
     * @return 危险单位ITEM信息
     */
    private PrppDangerItem copyNewPrppDangerItem(String endorseNo, ReinsEndorItem reinsEndorItemDto) {
        PrppDangerItem prppDangerItemDto = new PrppDangerItem();
        prppDangerItemDto.setEndorseNo(endorseNo);
        prppDangerItemDto.setRiskCode(reinsEndorItemDto.getRiskCode());
        prppDangerItemDto.setSerialNo(reinsEndorItemDto.getItemKindNo());
        prppDangerItemDto.setKindFlag("0");
        prppDangerItemDto.setItemCode(reinsEndorItemDto.getItemCode());
        prppDangerItemDto.setItemDetailName(reinsEndorItemDto.getItemDetailName());
        prppDangerItemDto.setKindCode(reinsEndorItemDto.getKindCode());
        prppDangerItemDto.setKindName(reinsEndorItemDto.getKindName());
        prppDangerItemDto.setCurrency(reinsEndorItemDto.getCurrency());
        //endorFlag是批改类型：I:新增,D:删除,U:变更,N:不变
        if (reinsEndorItemDto.getEndorFlag().startsWith(ReinsConst.ENDORFLAG_ADD)) {
            prppDangerItemDto.setAmount(ReinsConst.INIT_VALUE);
            prppDangerItemDto.setPremium(ReinsConst.INIT_VALUE);
            prppDangerItemDto.setPremOfVat(ReinsConst.INIT_VALUE);
            prppDangerItemDto.setVat(ReinsConst.INIT_VALUE);
            prppDangerItemDto.setPremFreeVat(ReinsConst.INIT_VALUE);
            prppDangerItemDto.setFreeVat(ReinsConst.INIT_VALUE);
        } else {
            prppDangerItemDto.setAmount(reinsEndorItemDto.getAmount());
            prppDangerItemDto.setPremium(reinsEndorItemDto.getPremium());
            prppDangerItemDto.setPremOfVat(reinsEndorItemDto.getPremOfVat());
            prppDangerItemDto.setVat(reinsEndorItemDto.getVat());
            prppDangerItemDto.setPremFreeVat(reinsEndorItemDto.getPremFreeVat());
            prppDangerItemDto.setFreeVat(reinsEndorItemDto.getFreeVat());
        }
        prppDangerItemDto.setChgAmount(reinsEndorItemDto.getChgAmount());
        prppDangerItemDto.setChgPremium(reinsEndorItemDto.getChgPremium());
        prppDangerItemDto.setChgPremOfVat(reinsEndorItemDto.getChgPremOfVat());
        prppDangerItemDto.setChgVat(reinsEndorItemDto.getChgVat());
        prppDangerItemDto.setChgPremFreeVat(reinsEndorItemDto.getChgPremFreeVat());
        prppDangerItemDto.setChgFreeVat(reinsEndorItemDto.getChgFreeVat());
        prppDangerItemDto.setCalculateFlag(reinsEndorItemDto.getCalculateFlag());
        prppDangerItemDto.setFlag(reinsEndorItemDto.getEndorFlag() + reinsEndorItemDto.getMainRiskFlag());
        prppDangerItemDto.setAddressCode(reinsEndorItemDto.getAddressCode());
        prppDangerItemDto.setAddressName(reinsEndorItemDto.getAddressName());
        prppDangerItemDto.setPostcode(reinsEndorItemDto.getPostcode());
        prppDangerItemDto.setExamineComCode(reinsEndorItemDto.getExamineComCode());
        prppDangerItemDto.setExamineUserCode(reinsEndorItemDto.getExamineUserCode());
        return prppDangerItemDto;
    }

    /**
     * 根据保单危险单位标的信息复制生成批单危险单位标的主要信息
     *
     * @param endorseNo         批单号
     * @param prpcDangerItemDto 保单危险单位标的
     * @return PrppDangerItemDto 批单危险单位标的
     */
    public PrppDangerItem copyPrppDangerItem(String endorseNo, PrpcDangerItem prpcDangerItemDto) {
        PrppDangerItem prppDangerItemDto = new PrppDangerItem();
        prppDangerItemDto.setEndorseNo(endorseNo);
        prppDangerItemDto.setRiskCode(prpcDangerItemDto.getRiskCode());
        prppDangerItemDto.setDangerNo(prpcDangerItemDto.getDangerNo());
        prppDangerItemDto.setKindFlag(prpcDangerItemDto.getKindFlag());
        prppDangerItemDto.setItemCode(prpcDangerItemDto.getItemCode());
        prppDangerItemDto.setItemDetailName(prpcDangerItemDto.getItemDetailName());
        prppDangerItemDto.setKindCode(prpcDangerItemDto.getKindCode());
        prppDangerItemDto.setKindName(prpcDangerItemDto.getKindName());
        prppDangerItemDto.setCurrency(prpcDangerItemDto.getCurrency());
        prppDangerItemDto.setAmount(prpcDangerItemDto.getAmount());
        prppDangerItemDto.setPremium(prpcDangerItemDto.getPremium());
        prppDangerItemDto.setChgAmount(ReinsConst.INIT_VALUE);
        prppDangerItemDto.setChgPremium(ReinsConst.INIT_VALUE);
        prppDangerItemDto.setCalculateFlag(prpcDangerItemDto.getCalculateFlag());
        prppDangerItemDto.setSerialNo(prpcDangerItemDto.getSerialNo());
        prppDangerItemDto.setPostcode(prpcDangerItemDto.getPostcode());
        prppDangerItemDto.setAddressName(prpcDangerItemDto.getAddressName());
        prppDangerItemDto.setAddressCode(prpcDangerItemDto.getAddressCode());
        prppDangerItemDto.setPremOfVat(prpcDangerItemDto.getPremOfVat());
        prppDangerItemDto.setVat(prpcDangerItemDto.getVat());
        prppDangerItemDto.setPremFreeVat(prpcDangerItemDto.getPremFreeVat());
        prppDangerItemDto.setFreeVat(prpcDangerItemDto.getFreeVat());
        prppDangerItemDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
        prppDangerItemDto.setChgVat(ReinsConst.INIT_VALUE);
        prppDangerItemDto.setChgPremFreeVat(ReinsConst.INIT_VALUE);
        prppDangerItemDto.setChgFreeVat(ReinsConst.INIT_VALUE);
        prppDangerItemDto.setExamineComCode(prpcDangerItemDto.getExamineComCode());
        prppDangerItemDto.setExamineUserCode(prpcDangerItemDto.getExamineUserCode());
        return prppDangerItemDto;
    }

    /**
     * 生成批单危险单位主表信息
     *
     * @param reinsEndorUnit 批单接口表基本信息
     * @param isFirstEndorse 是否第一次批改
     * @param preEndorseNo 上一次批单号
     * @return 危险单位金额信息
     */
    public Collection<PrppDangerDto> genDangerUnitInfo(ReinsEndorUnit reinsEndorUnit, boolean isFirstEndorse, String preEndorseNo) {
        Collection<PrppDangerDto> pDangerDtoList;
        if (!isFirstEndorse) {
            pDangerDtoList = this.getPreDangerByPreEndor(reinsEndorUnit, preEndorseNo);
        } else {
            pDangerDtoList = this.getPrpDangerByPolicy(reinsEndorUnit);
        }
        return pDangerDtoList;
    }

    /**
     * 非第一次批改生成批单危险单位主表信息
     *
     * @param reinsEndorUnit 批单接口表基本信息
     * @param preEndorseNo 上一次批单号
     * @return 危险单位金额信息
     */
    public Collection<PrppDangerDto> getPreDangerByPreEndor(ReinsEndorUnit reinsEndorUnit, String preEndorseNo) {
        Collection<PrppDangerDto> preDangerDtoList = this.getPreDangerList(preEndorseNo);
        String endorseNo = reinsEndorUnit.getEndorNo();
        for (PrppDangerDto pDangerDto : preDangerDtoList) {
            PrppDangerUnit prppDangerUnitDto = pDangerDto.getPrppDangerUnitDto();
            prppDangerUnitDto.setEndorseNo(endorseNo);
            prppDangerUnitDto.setAmount((ReinsUtils.add(prppDangerUnitDto.getAmount(), prppDangerUnitDto.getChgAmount())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerUnitDto.setPremium((ReinsUtils.add(prppDangerUnitDto.getPremium(), prppDangerUnitDto.getChgPremium())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerUnitDto.setDisFee((ReinsUtils.add(prppDangerUnitDto.getDisFee(), prppDangerUnitDto.getChgDisFee())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerUnitDto.setPremOfVat((ReinsUtils.add(prppDangerUnitDto.getPremOfVat(), prppDangerUnitDto.getChgPremOfVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerUnitDto.setPremFreeVat((ReinsUtils.add(prppDangerUnitDto.getPremFreeVat(), prppDangerUnitDto.getChgPremFreeVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerUnitDto.setVat((ReinsUtils.add(prppDangerUnitDto.getVat(), prppDangerUnitDto.getChgVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerUnitDto.setFreeVat((ReinsUtils.add(prppDangerUnitDto.getFreeVat(), prppDangerUnitDto.getChgFreeVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerUnitDto.setChgAmount(ReinsConst.INIT_VALUE);
            prppDangerUnitDto.setChgPremium(ReinsConst.INIT_VALUE);
            prppDangerUnitDto.setChgDisFee(ReinsConst.INIT_VALUE);
            prppDangerUnitDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
            prppDangerUnitDto.setChgVat(ReinsConst.INIT_VALUE);
            prppDangerUnitDto.setChgPremFreeVat(ReinsConst.INIT_VALUE);
            prppDangerUnitDto.setChgFreeVat(ReinsConst.INIT_VALUE);
            prppDangerUnitDto.setEndorseTimes(prppDangerUnitDto.getEndorseTimes() + 1);
            prppDangerUnitDto.setReinsureFlag("0");
            Collection<PrppDangerItem> prppDangerItemDtoList = pDangerDto.getPrppDangerItemDtoList();
            Collection<PrppDangerTot> prppDangerTotDtoList = pDangerDto.getPrppDangerTotDtoList();
            Collection<PrppDangerCoins> prppDangerCoinsDtoList = pDangerDto.getPrppDangerCoinsDtoList();
            Collection<PrppDangerPlan> prppDangerPlanDtoList = pDangerDto.getPrppDangerPlanDtoList();
            this.getPreDangerItem(prppDangerItemDtoList, endorseNo);
            this.getPreDangerTot(prppDangerTotDtoList, endorseNo);
            this.getPreDangerCoins(prppDangerCoinsDtoList, endorseNo);
            for (Object o : prppDangerPlanDtoList) {
                PrppDangerPlan prppDangerPlanDto = (PrppDangerPlan) o;
                prppDangerPlanDto.setEndorseNo(endorseNo);
            }
        }
        return preDangerDtoList;
    }

    /**
     * 生成危险单位初始共保信息
     *
     * @param prppDangerCoinsDtoList 批单危险单位共保信息
     * @param endorseNo 批单号
     */
    private void getPreDangerCoins(Collection<PrppDangerCoins> prppDangerCoinsDtoList, String endorseNo) {
        for (PrppDangerCoins prppDangerCoinsDto : prppDangerCoinsDtoList) {
            prppDangerCoinsDto.setEndorseNo(endorseNo);
            prppDangerCoinsDto.setCoinsAmount((ReinsUtils.add(prppDangerCoinsDto.getCoinsAmount(), prppDangerCoinsDto.getChgCoinsAmount())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerCoinsDto.setCoinsPremium((ReinsUtils.add(prppDangerCoinsDto.getCoinsPremium(), prppDangerCoinsDto.getChgCoinsPremium())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerCoinsDto.setPremOfVat((ReinsUtils.add(prppDangerCoinsDto.getPremOfVat(), prppDangerCoinsDto.getChgPremOfVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerCoinsDto.setVat((ReinsUtils.add(prppDangerCoinsDto.getVat(), prppDangerCoinsDto.getChgVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerCoinsDto.setPremFreeVat((ReinsUtils.add(prppDangerCoinsDto.getPremFreeVat(), prppDangerCoinsDto.getChgPremFreeVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerCoinsDto.setFreeVat((ReinsUtils.add(prppDangerCoinsDto.getFreeVat(), prppDangerCoinsDto.getChgFreeVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerCoinsDto.setChgCoinsAmount(ReinsConst.INIT_VALUE);
            prppDangerCoinsDto.setChgCoinsPremium(ReinsConst.INIT_VALUE);
            prppDangerCoinsDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
            prppDangerCoinsDto.setChgVat(ReinsConst.INIT_VALUE);
            prppDangerCoinsDto.setChgPremFreeVat(ReinsConst.INIT_VALUE);
            prppDangerCoinsDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
        }
    }

    /**
     * 生成批单危险单位初始tot信息
     *
     * @param prppDangerTotDtoList 批单危险单位金额信息
     * @param endorseNo 批单号
     */
    private void getPreDangerTot(Collection<PrppDangerTot> prppDangerTotDtoList, String endorseNo) {
        for (PrppDangerTot prppDangerTotDto : prppDangerTotDtoList) {
            prppDangerTotDto.setEndorseNo(endorseNo);
            prppDangerTotDto.setAmount((ReinsUtils.add(prppDangerTotDto.getAmount(), prppDangerTotDto.getChgAmount())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setPremium((ReinsUtils.add(prppDangerTotDto.getPremium(), prppDangerTotDto.getChgPremium())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setAmountEx((ReinsUtils.add(prppDangerTotDto.getAmountEx(), prppDangerTotDto.getChgAmountEx())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setPremiumEx((ReinsUtils.add(prppDangerTotDto.getPremiumEx(), prppDangerTotDto.getChgPremiumEx())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setPremOfVat((ReinsUtils.add(prppDangerTotDto.getPremOfVat(), prppDangerTotDto.getChgPremOfVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setVat((ReinsUtils.add(prppDangerTotDto.getVat(), prppDangerTotDto.getChgVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setPremFreeVat((ReinsUtils.add(prppDangerTotDto.getPremFreeVat(), prppDangerTotDto.getChgPremFreeVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setFreeVat((ReinsUtils.add(prppDangerTotDto.getFreeVat(), prppDangerTotDto.getChgFreeVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setPremFreeVatEx((ReinsUtils.add(prppDangerTotDto.getPremFreeVatEx(), prppDangerTotDto.getChgPremFreeVatEx())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setVatEx((ReinsUtils.add(prppDangerTotDto.getVatEx(), prppDangerTotDto.getChgVatEx())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setChgPremFreeVatEx((ReinsUtils.add(prppDangerTotDto.getPremFreeVatEx(), prppDangerTotDto.getChgPremFreeVatEx())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setFreeVatEx((ReinsUtils.add(prppDangerTotDto.getFreeVatEx(), prppDangerTotDto.getChgFreeVatEx())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerTotDto.setChgAmount(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgPremium(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgAmountEx(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgPremiumEx(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgVat(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgPremFreeVat(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgFreeVat(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgPremOfVatEx(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgVatEx(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgPremFreeVatEx(ReinsConst.INIT_VALUE);
            prppDangerTotDto.setChgFreeVatEx(ReinsConst.INIT_VALUE);
        }
    }

    /**
     * 生成初始批单危险单位item信息
     *
     * @param prppDangerItemDtoList 批单危险单位标的信息
     * @param endorseNo 批单号
     */
    private void getPreDangerItem(Collection<PrppDangerItem> prppDangerItemDtoList, String endorseNo) {
        for (PrppDangerItem prppDangerItemDto : prppDangerItemDtoList) {
            prppDangerItemDto.setEndorseNo(endorseNo);
            prppDangerItemDto.setAmount((ReinsUtils.add(prppDangerItemDto.getAmount(), prppDangerItemDto.getChgAmount())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerItemDto.setPremium((ReinsUtils.add(prppDangerItemDto.getPremium(), prppDangerItemDto.getChgPremium())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerItemDto.setPremOfVat((ReinsUtils.add(prppDangerItemDto.getPremOfVat(), prppDangerItemDto.getChgPremOfVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerItemDto.setVat((ReinsUtils.add(prppDangerItemDto.getVat(), prppDangerItemDto.getChgVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerItemDto.setPremFreeVat((ReinsUtils.add(prppDangerItemDto.getPremFreeVat(), prppDangerItemDto.getChgPremFreeVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerItemDto.setFreeVat((ReinsUtils.add(prppDangerItemDto.getFreeVat(), prppDangerItemDto.getChgFreeVat())).setScale(2,BigDecimal.ROUND_HALF_UP));
            prppDangerItemDto.setChgAmount(ReinsConst.INIT_VALUE);
            prppDangerItemDto.setChgPremium(ReinsConst.INIT_VALUE);
            prppDangerItemDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
            prppDangerItemDto.setChgVat(ReinsConst.INIT_VALUE);
            prppDangerItemDto.setChgPremFreeVat(ReinsConst.INIT_VALUE);
            prppDangerItemDto.setChgFreeVat(ReinsConst.INIT_VALUE);
        }
    }

    /**
     * 获取上一张批单危险单位各表信息
     *
     * @param preEndorseNo 上一张批单号
     * @return 批单危险单位对象
     */
    public Collection<PrppDangerDto> getPreDangerList(String preEndorseNo) {
        Collection<PrppDangerDto> preDangerDtoList = new ArrayList<>();
        QueryWrapper<PrppDangerUnit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ENDORSENO", preEndorseNo);
        queryWrapper.orderByAsc("DANGERNO");
        List<PrppDangerUnit> prepDangerUnitList = iPrppDangerUnitService.list(queryWrapper);
        if (ObjectUtil.isNotNull(prepDangerUnitList) && prepDangerUnitList.size() > 0) {
            for (PrppDangerUnit prepDangerUnitDto : prepDangerUnitList) {
                int dangerNo = prepDangerUnitDto.getDangerNo();
                PrppDangerDto pDangerDto = new PrppDangerDto();
                pDangerDto.setPrppDangerUnitDto(prepDangerUnitDto);
                Map<String, Object> columnMap = new HashMap<String, Object>(16) {{
                    put("ENDORSENO", preEndorseNo);
                    put("DANGERNO", dangerNo);
                }};
                pDangerDto.setPrppDangerItemDtoList(iPrppDangerItemService.listByMap(columnMap));
                pDangerDto.setPrppDangerCoinsDtoList(iPrppDangerCoinsService.listByMap(columnMap));
                pDangerDto.setPrppDangerPlanDtoList(iPrppDangerPlanService.listByMap(columnMap));
                pDangerDto.setPrppDangerTotDtoList(iPrppDangerTotService.listByMap(columnMap));
                preDangerDtoList.add(pDangerDto);
            }
        }
        return preDangerDtoList;
    }


    /**
     * 第一次批改生成批单危险单位主表信息
     * @param reinsEndorUnit 批单接口表基本信息
     * @return 批单危险单位金额信息
     */
    public Collection<PrppDangerDto> getPrpDangerByPolicy(ReinsEndorUnit reinsEndorUnit) {
        Collection<PrppDangerDto> preDangerDtoList = new ArrayList<>();
        Collection<PrpcDangerDto> prpcDangerDtoList = this.getPrpcDangerList(reinsEndorUnit.getPolicyNo());
        String endorseNo = reinsEndorUnit.getEndorNo();
        for (PrpcDangerDto prpcDangerDto : prpcDangerDtoList) {
            PrppDangerDto pDangerDto = new PrppDangerDto();
            PrpcDangerUnit prpcDangerUnitDto = prpcDangerDto.getPrpcDangerUnitDto();
            Collection<PrpcDangerItem> prpcDangerItemDtoList = prpcDangerDto.getPrpcDangerItemDtoList();
            Collection<PrpcDangerTot> prpcDangerTotDtoList = prpcDangerDto.getPrpcDangerTotDtoList();
            Collection<PrpcDangerCoins> prpcDangerCoinsDtoList = prpcDangerDto.getPrpcDangerCoinsDtoList();
            PrppDangerUnit prppDangerUnitDto = this.prpcDangerUnitToPrpeDangerUnit(endorseNo, prpcDangerUnitDto);
            Collection<PrppDangerItem> prppDangerItemDtoList = this.prpcDangerItemToPrpeDangerItem(endorseNo, prpcDangerItemDtoList);
            Collection<PrppDangerTot> prppDangerTotDtoList = this.prpcDangerTotToPrpeDangerTot(endorseNo, prpcDangerTotDtoList);
            Collection<PrppDangerCoins> prppDangerCoinsDtoList = this.prpcDangerCoinsToPrpeDangerCoins(endorseNo, prpcDangerCoinsDtoList);
            pDangerDto.setPrppDangerUnitDto(prppDangerUnitDto);
            pDangerDto.setPrppDangerItemDtoList(prppDangerItemDtoList);
            pDangerDto.setPrppDangerTotDtoList(prppDangerTotDtoList);
            pDangerDto.setPrppDangerCoinsDtoList(prppDangerCoinsDtoList);
            preDangerDtoList.add(pDangerDto);
        }
        return preDangerDtoList;
    }

    /**
     * 第一次批改生成批单共保初始信息
     *
     * @param endorseNo 批单号
     * @param prpcDangerCoinsDtoList 保单危险单位共保信息
     * @return 批单危险单位共保信息
     */
    private Collection<PrppDangerCoins> prpcDangerCoinsToPrpeDangerCoins(String endorseNo, Collection<PrpcDangerCoins> prpcDangerCoinsDtoList) {
        Collection<PrppDangerCoins> prppDangerCoinsDtoList = new ArrayList<>();
        if (ObjectUtil.isNotNull(prpcDangerCoinsDtoList) && prpcDangerCoinsDtoList.size()>0) {
            for (Object o : prpcDangerCoinsDtoList) {
                PrpcDangerCoins prpcDangerCoinsDto = (PrpcDangerCoins) o;
                PrppDangerCoins prppDangerCoinsDto = new PrppDangerCoins();
                prppDangerCoinsDto.setEndorseNo(endorseNo);
                prppDangerCoinsDto.setDangerNo(prpcDangerCoinsDto.getDangerNo());
                prppDangerCoinsDto.setMainEndorseNo(endorseNo);
                prppDangerCoinsDto.setSerialNo(prpcDangerCoinsDto.getSerialNo());
                prppDangerCoinsDto.setCoinsCode(prpcDangerCoinsDto.getCoinsCode());
                prppDangerCoinsDto.setCoinsName(prpcDangerCoinsDto.getCoinsName());
                prppDangerCoinsDto.setCoinsType(prpcDangerCoinsDto.getCoinsType());
                prppDangerCoinsDto.setCoinsRate(prpcDangerCoinsDto.getCoinsRate());
                prppDangerCoinsDto.setChiefFlag(prpcDangerCoinsDto.getChiefFlag());
                prppDangerCoinsDto.setProportionFlag(prpcDangerCoinsDto.getProportionFlag());
                prppDangerCoinsDto.setCurrency(prpcDangerCoinsDto.getCurrency());
                prppDangerCoinsDto.setCoinsAmount(prpcDangerCoinsDto.getCoinsAmount());
                prppDangerCoinsDto.setCoinsPremium(prpcDangerCoinsDto.getCoinsPremium());
                prppDangerCoinsDto.setMiddleCostFee(prpcDangerCoinsDto.getMiddleCostFee());
                prppDangerCoinsDto.setChgCoinsAmount(ReinsConst.INIT_VALUE);
                prppDangerCoinsDto.setChgCoinsPremium(ReinsConst.INIT_VALUE);
                prppDangerCoinsDto.setPremOfVat(prpcDangerCoinsDto.getPremOfVat());
                prppDangerCoinsDto.setVat(prpcDangerCoinsDto.getVat());
                prppDangerCoinsDto.setPremFreeVat(prpcDangerCoinsDto.getPremFreeVat());
                prppDangerCoinsDto.setFreeVat(prpcDangerCoinsDto.getFreeVat());
                prppDangerCoinsDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
                prppDangerCoinsDto.setChgVat(ReinsConst.INIT_VALUE);
                prppDangerCoinsDto.setChgPremFreeVat(ReinsConst.INIT_VALUE);
                prppDangerCoinsDto.setChgFreeVat(ReinsConst.INIT_VALUE);
                prppDangerCoinsDto.setMiddleCostFee(ReinsConst.INIT_VALUE);
                prppDangerCoinsDto.setFlag("");
                prppDangerCoinsDto.setExamineComCode(prpcDangerCoinsDto.getExamineComCode());
                prppDangerCoinsDto.setExamineUserCode(prpcDangerCoinsDto.getExamineUserCode());
                prppDangerCoinsDtoList.add(prppDangerCoinsDto);
            }
        }
        return prppDangerCoinsDtoList;
    }

    /**
     * 第一次批改生成批单tot信息
     *
     * @param endorseNo 批单号
     * @param prpcDangerTotDtoList 保单危险单位金额信息
     * @return 批单危险单位金额信息
     */
    private Collection<PrppDangerTot> prpcDangerTotToPrpeDangerTot(String endorseNo, Collection<PrpcDangerTot> prpcDangerTotDtoList) {
        Collection<PrppDangerTot> prppDangerTotDtoList = new ArrayList<>();
        if (ObjectUtil.isNotNull(prpcDangerTotDtoList) && prpcDangerTotDtoList.size()>0) {
            for (PrpcDangerTot prpcDangerTotDto : prpcDangerTotDtoList) {
                PrppDangerTot prppDangerTotDto = new PrppDangerTot();
                prppDangerTotDto.setEndorseNo(endorseNo);
                prppDangerTotDto.setDangerNo(prpcDangerTotDto.getDangerNo());
                prppDangerTotDto.setSCurrency(prpcDangerTotDto.getSCurrency());
                prppDangerTotDto.setAmount(prpcDangerTotDto.getAmount());
                prppDangerTotDto.setPremium(prpcDangerTotDto.getPremium());
                prppDangerTotDto.setPremOfVat(prpcDangerTotDto.getPremOfVat());
                prppDangerTotDto.setVat(prpcDangerTotDto.getVat());
                prppDangerTotDto.setPremFreeVat(prpcDangerTotDto.getPremFreeVat());
                prppDangerTotDto.setFreeVat(prpcDangerTotDto.getFreeVat());
                prppDangerTotDto.setChgAmount(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setChgPremium(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setChgVat(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setChgPremFreeVat(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setChgFreeVat(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setTCurrency(prpcDangerTotDto.getTCurrency());
                prppDangerTotDto.setExchRate(prpcDangerTotDto.getExchRate());
                prppDangerTotDto.setAmountEx(prpcDangerTotDto.getAmountEx());
                prppDangerTotDto.setPremiumEx(prpcDangerTotDto.getPremiumEx());
                prppDangerTotDto.setPremOfVatEx(prpcDangerTotDto.getPremOfVatEx());
                prppDangerTotDto.setVatEx(prpcDangerTotDto.getVatEx());
                prppDangerTotDto.setPremFreeVatEx(prpcDangerTotDto.getPremFreeVatEx());
                prppDangerTotDto.setFreeVatEx(prpcDangerTotDto.getFreeVatEx());
                prppDangerTotDto.setChgAmountEx(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setChgPremiumEx(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setChgPremOfVatEx(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setChgVatEx(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setChgPremFreeVatEx(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setChgFreeVatEx(ReinsConst.INIT_VALUE);
                prppDangerTotDto.setFlag(prpcDangerTotDto.getFlag());
                prppDangerTotDto.setExamineComCode(prpcDangerTotDto.getExamineComCode());
                prppDangerTotDto.setExamineUserCode(prpcDangerTotDto.getExamineUserCode());
                prppDangerTotDtoList.add(prppDangerTotDto);
            }
        }
        return prppDangerTotDtoList;
    }

    /**
     * 第一次批改生成批单item子表信息
     *
     * @param endorseNo 批单号
     * @param prpcDangerItemDtoList 保单危险单位标的信息
     * @return 批单危险单位标的信息
     */
    private Collection<PrppDangerItem> prpcDangerItemToPrpeDangerItem(String endorseNo, Collection<PrpcDangerItem> prpcDangerItemDtoList) {
        Collection<PrppDangerItem> prppDangerItemDtoList = new ArrayList<>();
        if (ObjectUtil.isNotNull(prpcDangerItemDtoList) && prpcDangerItemDtoList.size()>0) {
            for (PrpcDangerItem prpcDangerItemDto : prpcDangerItemDtoList) {
                PrppDangerItem prppDangerItemDto = new PrppDangerItem();
                prppDangerItemDto.setEndorseNo(endorseNo);
                prppDangerItemDto.setRiskCode(prpcDangerItemDto.getRiskCode());
                prppDangerItemDto.setDangerNo(prpcDangerItemDto.getDangerNo());
                prppDangerItemDto.setKindFlag(prpcDangerItemDto.getKindFlag());
                prppDangerItemDto.setItemCode(prpcDangerItemDto.getItemCode());
                prppDangerItemDto.setItemDetailName(prpcDangerItemDto.getItemDetailName());
                prppDangerItemDto.setKindCode(prpcDangerItemDto.getKindCode());
                prppDangerItemDto.setKindName(prpcDangerItemDto.getKindName());
                prppDangerItemDto.setCurrency(prpcDangerItemDto.getCurrency());
                prppDangerItemDto.setAmount(prpcDangerItemDto.getAmount());
                prppDangerItemDto.setPremium(prpcDangerItemDto.getPremium());
                prppDangerItemDto.setChgAmount(ReinsConst.INIT_VALUE);
                prppDangerItemDto.setChgPremium(ReinsConst.INIT_VALUE);
                prppDangerItemDto.setCalculateFlag(prpcDangerItemDto.getCalculateFlag());
                prppDangerItemDto.setSerialNo(prpcDangerItemDto.getSerialNo());
                prppDangerItemDto.setPostcode(prpcDangerItemDto.getPostcode());
                prppDangerItemDto.setAddressCode(prpcDangerItemDto.getAddressCode());
                prppDangerItemDto.setAddressName(prpcDangerItemDto.getAddressName());
                prppDangerItemDto.setInsuredName(prpcDangerItemDto.getInsuredName());
                prppDangerItemDto.setIdentifyNumber(prpcDangerItemDto.getIdentifyNumber());
                prppDangerItemDto.setPremOfVat(prpcDangerItemDto.getPremOfVat());
                prppDangerItemDto.setVat(prpcDangerItemDto.getVat());
                prppDangerItemDto.setPremFreeVat(prpcDangerItemDto.getPremFreeVat());
                prppDangerItemDto.setFreeVat(prpcDangerItemDto.getFreeVat());
                prppDangerItemDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
                prppDangerItemDto.setChgVat(ReinsConst.INIT_VALUE);
                prppDangerItemDto.setChgPremFreeVat(ReinsConst.INIT_VALUE);
                prppDangerItemDto.setChgFreeVat(ReinsConst.INIT_VALUE);
                prppDangerItemDto.setExamineComCode(prpcDangerItemDto.getExamineComCode());
                prppDangerItemDto.setExamineUserCode(prpcDangerItemDto.getExamineUserCode());
                prppDangerItemDtoList.add(prppDangerItemDto);
            }
        }

        return prppDangerItemDtoList;
    }

    /**
     * 第一次批改生成批单主表初始信息
     *
     * @param endorseNo 批单号
     * @param prpcDangerUnitDto 保单危险单位基本信息
     * @return 批单危险单位基本信息
     */
    private PrppDangerUnit prpcDangerUnitToPrpeDangerUnit(String endorseNo, PrpcDangerUnit prpcDangerUnitDto) {
        PrppDangerUnit prppDangerUnitDto = new PrppDangerUnit();
        prppDangerUnitDto.setEndorseNo(endorseNo);
        prppDangerUnitDto.setPolicyNo(prpcDangerUnitDto.getPolicyNo());
        prppDangerUnitDto.setRiskCode(prpcDangerUnitDto.getRiskCode());
        prppDangerUnitDto.setDangerNo(prpcDangerUnitDto.getDangerNo());
        prppDangerUnitDto.setDangerDesc(prpcDangerUnitDto.getDangerDesc());
        prppDangerUnitDto.setAddressCode(prpcDangerUnitDto.getAddressCode());
        prppDangerUnitDto.setAddressName(prpcDangerUnitDto.getAddressName());
        prppDangerUnitDto.setRiskLevel(prpcDangerUnitDto.getRiskLevel());
        prppDangerUnitDto.setRiskLevelDesc(prpcDangerUnitDto.getRiskLevelDesc());
        prppDangerUnitDto.setRiskClass(prpcDangerUnitDto.getRiskClass());
        prppDangerUnitDto.setRiskClassDesc(prpcDangerUnitDto.getRiskClassDesc());
        prppDangerUnitDto.setItemKind(prpcDangerUnitDto.getItemKind());
        prppDangerUnitDto.setItemKindDesc(prpcDangerUnitDto.getItemKindDesc());
        prppDangerUnitDto.setCurrency(prpcDangerUnitDto.getCurrency());
        prppDangerUnitDto.setAmount(prpcDangerUnitDto.getAmount());
        prppDangerUnitDto.setPremium(prpcDangerUnitDto.getPremium());
        prppDangerUnitDto.setDisFee(prpcDangerUnitDto.getDisFee());
        prppDangerUnitDto.setChgAmount(ReinsConst.INIT_VALUE);
        prppDangerUnitDto.setChgPremium(ReinsConst.INIT_VALUE);
        prppDangerUnitDto.setChgDisFee(ReinsConst.INIT_VALUE);
        prppDangerUnitDto.setDangerShare(prpcDangerUnitDto.getDangerShare());
        prppDangerUnitDto.setSpeCurrency(prpcDangerUnitDto.getSpeCurrency());
        prppDangerUnitDto.setSpeRate(prpcDangerUnitDto.getSpeRate());
        prppDangerUnitDto.setSpeValue(prpcDangerUnitDto.getSpeValue());
        prppDangerUnitDto.setRetCurrency(prpcDangerUnitDto.getRetCurrency());
        prppDangerUnitDto.setRetentionValue(prpcDangerUnitDto.getRetentionValue());
        prppDangerUnitDto.setReMarks(prpcDangerUnitDto.getReMarks());
        prppDangerUnitDto.setFlag(prpcDangerUnitDto.getFlag());
        prppDangerUnitDto.setEndorseTimes(0);
        prppDangerUnitDto.setReinsureFlag("0");
        prppDangerUnitDto.setGroupFlag(prpcDangerUnitDto.getGroupFlag());
        prppDangerUnitDto.setCoinsFlag(prpcDangerUnitDto.getCoinsFlag());
        prppDangerUnitDto.setShareholderFlag(prpcDangerUnitDto.getShareholderFlag());
        prppDangerUnitDto.setBusinessFlag(prpcDangerUnitDto.getBusinessFlag());
        prppDangerUnitDto.setShipCode(prpcDangerUnitDto.getShipCode());
        prppDangerUnitDto.setStartDate(prpcDangerUnitDto.getStartDate());
        prppDangerUnitDto.setEndDate(prpcDangerUnitDto.getEndDate());
        prppDangerUnitDto.setBusinessChannel(prpcDangerUnitDto.getBusinessChannel());
        prppDangerUnitDto.setBusinessNature(prpcDangerUnitDto.getBusinessNature());
        prppDangerUnitDto.setFlag1(prpcDangerUnitDto.getFlag1());
        prppDangerUnitDto.setPremOfVat(prpcDangerUnitDto.getPremOfVat());
        prppDangerUnitDto.setVat(prpcDangerUnitDto.getVat());
        prppDangerUnitDto.setVatRate(prpcDangerUnitDto.getVatRate());
        prppDangerUnitDto.setPremFreeVat(prpcDangerUnitDto.getPremFreeVat());
        prppDangerUnitDto.setFreeVat(prpcDangerUnitDto.getFreeVat());
        prppDangerUnitDto.setChgPremOfVat(ReinsConst.INIT_VALUE);
        prppDangerUnitDto.setChgVat(ReinsConst.INIT_VALUE);
        prppDangerUnitDto.setChgPremFreeVat(ReinsConst.INIT_VALUE);
        prppDangerUnitDto.setChgFreeVat(ReinsConst.INIT_VALUE);
        prppDangerUnitDto.setExamineComCode(prpcDangerUnitDto.getExamineComCode());
        prppDangerUnitDto.setExamineUserCode(prpcDangerUnitDto.getExamineUserCode());
        return prppDangerUnitDto;
    }

    /**
     * 批单第一次批改获取保单初始信息
     *
     * @param policyNo 保单号
     * @return 保单危险单位对象
     */
    public Collection<PrpcDangerDto> getPrpcDangerList(String policyNo) {
        Collection<PrpcDangerDto> prcDangerDtoList = new ArrayList<>();
        QueryWrapper<PrpcDangerUnit> qw = new QueryWrapper<>();
        qw.eq("POLICYNO", policyNo);
        qw.orderByAsc("DANGERNO");
        Collection<PrpcDangerUnit> prpcDangerUnitDtoList = iPrpcDangerUnitService.list(qw);
        if (ObjectUtil.isNotNull(prpcDangerUnitDtoList) && prpcDangerUnitDtoList.size() > 0) {
            for (PrpcDangerUnit prpcDangerUnitDto : prpcDangerUnitDtoList) {
                int dangerNo = prpcDangerUnitDto.getDangerNo();
                PrpcDangerDto cDangerDto = new PrpcDangerDto();
                cDangerDto.setPrpcDangerUnitDto(prpcDangerUnitDto);
                Map<String, Object> columnMap = new HashMap<String, Object>(16) {{
                    put("POLICYNO", policyNo);
                    put("DANGERNO", dangerNo);
                }};
                cDangerDto.setPrpcDangerItemDtoList(iPrpcDangerItemService.listByMap(columnMap));
                cDangerDto.setPrpcDangerCoinsDtoList(iPrpcDangerCoinsService.listByMap(columnMap));
                cDangerDto.setPrpcDangerPlanDtoList(iPrpcDangerPlanService.listByMap(columnMap));
                cDangerDto.setPrpcDangerTotDtoList(iPrpcDangerTotService.listByMap(columnMap));
                prcDangerDtoList.add(cDangerDto);
            }
        }
        return prcDangerDtoList;
    }

    /**
     * 获取上一张批单业务号
     *
     * @param endorNo  批单号
     * @param policyNo 保单号
     * @return 上一张批单号
     */
    public String getPreviousEndorseNo(String endorNo, String policyNo) {
        return reinsEndorUnitService.getPreEndorNo(endorNo, policyNo);
    }


    /**
     * 校验是否是第一次批改
     * @param endorNo 批单号
     * @param policyNo 保单号
     * @return 是否是第一次批改
     */
    public boolean isFirstEndorse(String endorNo, String policyNo) {
        boolean flag = false;
        Collection<ReinsPolicyUnit> reinsPolicyUnitList = iReinsPolicyUnitService.reinsPolicyUnitList(policyNo, endorNo);
        if (ObjectUtil.isNull(reinsPolicyUnitList) || reinsPolicyUnitList.isEmpty()) {
            flag = true;
        }
        return flag;
    }

    /**
     * 保存接口信息
     *
     * @param reinsEndorInterfDto 批单接口表对象
     */
    public void saveReinsEndorInterf(ReinsEndorInterfDto reinsEndorInterfDto) {
        if (ObjectUtil.isNotNull(reinsEndorInterfDto.getReinsEndorUnitDto())) {
            reinsEndorUnitService.save(reinsEndorInterfDto.getReinsEndorUnitDto());
        }
        if (ObjectUtil.isNotNull(reinsEndorInterfDto.getReinsEndorItemDto())  && reinsEndorInterfDto.getReinsEndorItemDto().size() > 0) {
            reinsEndorItemService.saveBatch(reinsEndorInterfDto.getReinsEndorItemDto());
        }
        if (ObjectUtil.isNotNull(reinsEndorInterfDto.getReinsEndorCededDto()) && reinsEndorInterfDto.getReinsEndorCededDto().size() > 0) {
            reinsEndorCededService.saveBatch(reinsEndorInterfDto.getReinsEndorCededDto());
        }
        if (ObjectUtil.isNotNull(reinsEndorInterfDto.getReinsEndorCoinsDto()) && reinsEndorInterfDto.getReinsEndorCoinsDto().size() > 0) {
            reinsEndorCoinsService.saveBatch(reinsEndorInterfDto.getReinsEndorCoinsDto());
        }
        if (ObjectUtil.isNotNull(reinsEndorInterfDto.getReinsEndorEngageDto()) && reinsEndorInterfDto.getReinsEndorEngageDto().size() > 0) {
            reinsEndorEngageService.saveBatch(reinsEndorInterfDto.getReinsEndorEngageDto());
        }
        if (ObjectUtil.isNotNull(reinsEndorInterfDto.getReinsEndorFeeDto() ) && reinsEndorInterfDto.getReinsEndorFeeDto().size() > 0) {
            reinsEndorFeeService.saveBatch(reinsEndorInterfDto.getReinsEndorFeeDto());
        }
        if (ObjectUtil.isNotNull(reinsEndorInterfDto.getReinsEndorLimitDto()) && reinsEndorInterfDto.getReinsEndorLimitDto().size() > 0) {
            reinsEndorLimitService.saveBatch(reinsEndorInterfDto.getReinsEndorLimitDto());
        }
    }

    /**
     * 删除接口信息
     *
     * @param cerTiNo 业务号
     */
    @Override
    public void deleteReinsEndorInterf(String cerTiNo) {

        Map<String, Object> columnMap = new HashMap<String, Object>(16) {{
            put("ENDORNO", cerTiNo);
        }};
        reinsEndorItemService.removeByMap(columnMap);
        reinsEndorCededService.removeByMap(columnMap);
        reinsEndorCoinsService.removeByMap(columnMap);
        reinsEndorEngageService.removeByMap(columnMap);
        reinsEndorFeeService.removeByMap(columnMap);
        reinsEndorLimitService.removeByMap(columnMap);
        reinsEndorUnitService.removeByMap(columnMap);
    }


    /**
     * 初始化接口返回信息
     * @param cerTiType 业务类型
     * @param endorNo 业务编码
     * @return 接口返回信息
     */
    public ReinsStatusDto generateReinsStatus(String cerTiType,
                                              String endorNo) {
        ReinsStatusDto reinsStatusDto = new ReinsStatusDto();
        reinsStatusDto.setCerTiNo(endorNo);
        reinsStatusDto.setCerTiType(cerTiType);
        reinsStatusDto.setStatus("Y");
        return reinsStatusDto;
    }

    /**
     * 复制原保单的询价单信息(提交核批时根据保单的询价单来生成批单的询价单中间表替换)
     *
     * @param pDangerDtoList 批单危险单位对象
     * @param reinsEndorUnit 批单接口表基本信息
     * @param isFirstEndorse 是否第一次批改
     * @param preEndorseNo   上一次批改批单号
     */
    private void copyFeoEnquiryToEndorse(Collection<PrppDangerDto> pDangerDtoList, ReinsEndorUnit reinsEndorUnit, boolean isFirstEndorse, String preEndorseNo) {
        //删除原有数据
        this.deleteFeoEnquiryEndorse(reinsEndorUnit.getEndorNo());
        //提交核批时根据保单的询价单来生成批单的询价单中间表替换
        Map<String, Object> strConditionMap = new HashMap<>(16);
        if (isFirstEndorse) {
            strConditionMap.put("POLICYNO", reinsEndorUnit.getPolicyNo());
        } else {
            strConditionMap.put("ENDORSENO", preEndorseNo);
        }
        for (PrppDangerDto prppDangerDto : pDangerDtoList) {
            PrppDangerUnit prppDangerUnit = prppDangerDto.getPrppDangerUnitDto();
            strConditionMap.put("DANGERNO", prppDangerUnit.getDangerNo());
            List<FeoEnquiry> feoEnquiryList = iFeoEnquiryService.listByMap(strConditionMap);
            if (feoEnquiryList == null || feoEnquiryList.size() == 0) {
                // 保单或上一次批单没有询价单，直接退出
                continue;
            }
            EnquiryDto enquiryDto = new EnquiryDto();
            FeoEnquiry feoEnquiry = feoEnquiryList.iterator().next();
            String oldEnquiryNo = feoEnquiry.getEnquiryNo();
            // 复制原保单的询价单信息主表
            genCopyFeoEnquiry(prppDangerUnit, reinsEndorUnit, feoEnquiry);
            enquiryDto.setFeoEnquiry(feoEnquiry);
            // 设置feoItem
            this.genPrppFeoCoins(enquiryDto, prppDangerDto.getPrppDangerCoinsDtoList());
            this.genPrppFeoItem(enquiryDto, prppDangerDto.getPrppDangerItemDtoList());
            this.genPrppFeoTot(enquiryDto, prppDangerDto.getPrppDangerTotDtoList());

            Map<String, Object> columnMap = new HashMap<>(16);
            columnMap.put("ENQUIRYNO", oldEnquiryNo);
            Collection<FeoReinsReceive> feoReinsReceiveList = iFeoReinsReceiveService.listByMap(columnMap);
            Collection<FeoReinsReceive> feoReinsReceiveListNew = new ArrayList<>();
            for (FeoReinsReceive feoReinsReceive : feoReinsReceiveList) {
                feoReinsReceive.setEnquiryNo(feoEnquiry.getEnquiryNo());
                feoReinsReceive.setEndorseNo(feoEnquiry.getEndorseNo());
                //增加批单临分差异临分处理保额保费
                BigDecimal shareRate = feoReinsReceive.getSignedLine();
                BigDecimal amount = feoReinsReceive.getAmount();
                BigDecimal baseAmount = feoEnquiry.getSumAmount().multiply(feoEnquiry.getBaseShare()).divide(
                        new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP);
                //差异分保处理
                if (feoReinsReceive.getFlag().startsWith("1")) {
                    if (enquiryDto.getFeoEnquiry().getStartDate() != feoReinsReceive.getStartDate()) {
                        feoReinsReceive.setAmount(ReinsUtils.rateMulRound(baseAmount, shareRate, 100, 2));
                        //先计算变化量 拿保额变化量乘以保单的费率 然后通过 变化量计算批改后总分出保费
                        feoReinsReceive.setChgAmount(ReinsUtils.rateMulRound(baseAmount, shareRate, 100, 2).subtract(amount));
                        feoReinsReceive.setChgPremium(ReinsUtils.rateMulRound(feoReinsReceive.getChgAmount(), feoEnquiry.getRate(), 1000, 2));
                        feoReinsReceive.setPremium(ReinsUtils.addRound(feoReinsReceive.getPremium(), feoReinsReceive.getChgPremium(), 2));
                        if (feoReinsReceive.getAmount().doubleValue() != 0) {
                            feoReinsReceive.setRate(ReinsUtils.mulRound(feoReinsReceive.getPremium().divide(feoReinsReceive.getAmount(), 9, BigDecimal.ROUND_HALF_UP), new BigDecimal("1000"), 4));
                        } else {
                            feoReinsReceive.setRate(BigDecimal.ZERO);
                        }
                    } else {
                        feoReinsReceive.setAmount(ReinsUtils.rateMulRound(baseAmount, shareRate, 100, 2));
                        feoReinsReceive.setChgAmount(ReinsUtils.rateMulRound(baseAmount, shareRate, 100, 2).subtract(amount));
                        feoReinsReceive.setPremium(ReinsUtils.rateMulRound(feoReinsReceive.getAmount(), feoReinsReceive.getRate(), 1000, 2));
                        feoReinsReceive.setChgPremium(ReinsUtils.rateMulRound(feoReinsReceive.getChgAmount(), feoReinsReceive.getRate(), 1000, 2));
                    }
                }

                feoReinsReceiveListNew.add(feoReinsReceive);
            }
            enquiryDto.setFeoReinsReceiveList(feoReinsReceiveListNew);
            // 临分协议存在投保单进了临分协议，但是批单金额超过临分协议限额，无法进临分协议的情况，此种情况不生成临分询价单相关信息 begin
            this.saveEnquiryDto(enquiryDto);
        }
    }

    /**
     * 询价单保存
     *
     * @param enquiryDto 询价单信息对象
     */
    private void saveEnquiryDto(EnquiryDto enquiryDto) {
        Map<String, Object> columnMap = new HashMap<String, Object>(16) {{
            put("enquiryNo", enquiryDto.getFeoEnquiry().getEnquiryNo());
        }};
        iFeoCoinsService.removeByMap(columnMap);
        iFeoItemService.removeByMap(columnMap);
        iFeoTotService.removeByMap(columnMap);
        iFeoEngageService.removeByMap(columnMap);
        iFeoReinsReceiveService.removeByMap(columnMap);
        Collection<FeoReinsVerify> feoReinsVerifyList = iFeoReinsVerifyService.listByMap(columnMap);
        iFeoReinsVerifyService.removeByMap(columnMap);
        iFeoEnquiryService.removeByMap(columnMap);
        if (enquiryDto.getFeoEnquiry() == null) {
            return;
        }
        int count = 1;
        //插入数据
        iFeoEnquiryService.save(enquiryDto.getFeoEnquiry());
        if (enquiryDto.getFeoItemList() != null) {
            iFeoItemService.saveBatch(enquiryDto.getFeoItemList());
        }
        if (enquiryDto.getFeoTotList() != null) {
            iFeoTotService.saveBatch(enquiryDto.getFeoTotList());
        }
        if (enquiryDto.getFeoEngageList() != null) {
            iFeoEngageService.saveBatch(enquiryDto.getFeoEngageList());
        }
        if (enquiryDto.getFeoCoinsList() != null) {
            iFeoCoinsService.saveBatch(enquiryDto.getFeoCoinsList());
        }
        if (enquiryDto.getFeoReinsReceiveList() != null) {
            iFeoReinsReceiveService.saveBatch(enquiryDto.getFeoReinsReceiveList());
        }
        if (feoReinsVerifyList != null) {
            iFeoReinsVerifyService.saveBatch(feoReinsVerifyList);
            count = feoReinsVerifyList.size() + 1;
        }
        if (enquiryDto.getFeoReinsVerify() != null) {
            FeoReinsVerify feoReinsVerifyDto = enquiryDto.getFeoReinsVerify();
            feoReinsVerifyDto.setSerialNo(count);
            iFeoReinsVerifyService.save(feoReinsVerifyDto);
        }
    }

    /**
     * 预约协议批单询价单生成 feoTot
     *
     * @param enquiryDto           询价单信息对象
     * @param prppDangerTotDtoList 危险单位标的信息
     */
    private void genPrppFeoTot(EnquiryDto enquiryDto, Collection<PrppDangerTot> prppDangerTotDtoList) {

        if (ObjectUtil.isNotNull(prppDangerTotDtoList)) {
            List<FeoTot> feoTotDtoList = new ArrayList<>();
            for (PrppDangerTot prppDangerTot : prppDangerTotDtoList) {
                FeoTot feoTot = new FeoTot();
                feoTot.setEnquiryNo(enquiryDto.getFeoEnquiry().getEnquiryNo());
                feoTot.setSCurrency(prppDangerTot.getSCurrency());
                feoTot.setProposalNo(enquiryDto.getFeoEnquiry().getProposalNo());
                feoTot.setPolicyNo(enquiryDto.getFeoEnquiry().getPolicyNo());
                feoTot.setEndorseNo(enquiryDto.getFeoEnquiry().getEndorseNo());
                feoTot.setDangerNo(prppDangerTot.getDangerNo());
                feoTot.setRiskCode(enquiryDto.getFeoEnquiry().getRiskCode());
                feoTot.setAmount(ReinsUtils.addRound(prppDangerTot.getAmount(), prppDangerTot.getChgAmount(), 2));
                //修改询价单保额保费为批改后的保额保费
                feoTot.setPremium(ReinsUtils.addRound(prppDangerTot.getPremium(), prppDangerTot.getChgPremium(), 2));
                feoTot.setExchRate(prppDangerTot.getExchRate());
                feoTot.setTCurrency(prppDangerTot.getTCurrency());
                feoTot.setAmountEx(ReinsUtils.addRound(prppDangerTot.getAmountEx(), prppDangerTot.getChgAmountEx(), 2));
                feoTot.setPremiumEx(ReinsUtils.addRound(prppDangerTot.getPremiumEx(), prppDangerTot.getChgPremiumEx(), 2));
                feoTotDtoList.add(feoTot);
            }
            enquiryDto.setFeoTotList(feoTotDtoList);
        }
    }

    /**
     * 预约协议批单询价单生成 feoItem
     *
     * @param enquiryDto            询价单信息对象
     * @param prppDangerItemDtoList 危险单位标的信息
     */
    private void genPrppFeoItem(EnquiryDto enquiryDto, Collection<PrppDangerItem> prppDangerItemDtoList) {
        if (ObjectUtil.isNotNull(prppDangerItemDtoList)) {
            List<FeoItem> feoItemDtoList = new ArrayList<>();
            for (PrppDangerItem prppDangerItem : prppDangerItemDtoList) {
                FeoItem feoItem = new FeoItem();
                feoItem.setEnquiryNo(enquiryDto.getFeoEnquiry().getEnquiryNo());
                feoItem.setSerialNo(prppDangerItem.getSerialNo());
                feoItem.setProposalNo(enquiryDto.getFeoEnquiry().getProposalNo());
                feoItem.setPolicyNo(enquiryDto.getFeoEnquiry().getPolicyNo());
                feoItem.setEndorseNo(enquiryDto.getFeoEnquiry().getEndorseNo());
                feoItem.setDangerNo(prppDangerItem.getDangerNo());
                feoItem.setRiskCode(prppDangerItem.getRiskCode());
                feoItem.setKindCode(prppDangerItem.getKindCode());
                feoItem.setKindName(prppDangerItem.getKindName());
                feoItem.setItemName(prppDangerItem.getItemDetailName());
                feoItem.setItemAddress(prppDangerItem.getAddressCode());
                feoItem.setCurrency(prppDangerItem.getCurrency());
                //修改询价单保额保费为批改后的保额保费
                feoItem.setAmount(ReinsUtils.addRound(prppDangerItem.getAmount(), prppDangerItem.getChgAmount(), 2));
                feoItem.setPremium(ReinsUtils.addRound(prppDangerItem.getPremium(), prppDangerItem.getChgPremium(), 2));

                if (ReinsUtils.addRound(prppDangerItem.getAmount(), prppDangerItem.getChgAmount(), 2).doubleValue() != 0) {
                    feoItem.setRate(ReinsUtils.add(prppDangerItem.getPremium(), prppDangerItem.getChgPremium()).multiply(BigDecimal.valueOf(1000)).divide(
                            ReinsUtils.add(prppDangerItem.getAmount(), prppDangerItem.getChgAmount()), 4, BigDecimal.ROUND_HALF_UP));
                } else {
                    feoItem.setRate(BigDecimal.ZERO);
                }
                feoItem.setCalculateFlag(prppDangerItem.getCalculateFlag());
                feoItemDtoList.add(feoItem);
            }
            enquiryDto.setFeoItemList(feoItemDtoList);
        }

    }

    /**
     * 预约协议批单询价单生成 feoCoins
     *
     * @param enquiryDto             询价单信息对象
     * @param prppDangerCoinsDtoList 危险单位共保信息
     */
    private void genPrppFeoCoins(EnquiryDto enquiryDto, Collection<PrppDangerCoins> prppDangerCoinsDtoList) {

        if (ObjectUtil.isNotNull(prppDangerCoinsDtoList)) {
            List<FeoCoins> feoCoinsDtoList = new ArrayList<>();
            for (PrppDangerCoins prppDangerCoins : prppDangerCoinsDtoList) {
                FeoCoins feoCoinsDto = new FeoCoins();
                feoCoinsDto.setProposalNo(enquiryDto.getFeoEnquiry().getProposalNo());
                feoCoinsDto.setPolicyNo(enquiryDto.getFeoEnquiry().getPolicyNo());
                feoCoinsDto.setEndorseNo(enquiryDto.getFeoEnquiry().getEndorseNo());
                feoCoinsDto.setDangerNo(enquiryDto.getFeoEnquiry().getDangerNo());
                feoCoinsDto.setEnquiryNo(enquiryDto.getFeoEnquiry().getEnquiryNo());
                feoCoinsDto.setSerialNo(prppDangerCoins.getSerialNo());
                feoCoinsDto.setCoinsCode(prppDangerCoins.getCoinsCode());
                feoCoinsDto.setCoinsName(prppDangerCoins.getCoinsName());
                feoCoinsDto.setCoinsType(prppDangerCoins.getCoinsType());
                feoCoinsDto.setCoinsRate(prppDangerCoins.getCoinsRate());
                feoCoinsDto.setChiefFlag(prppDangerCoins.getChiefFlag());
                feoCoinsDto.setProportionFlag(prppDangerCoins.getProportionFlag());
                feoCoinsDto.setCurrency(prppDangerCoins.getCurrency());
                //修改询价单保额保费为批改后的保额保费
                feoCoinsDto.setCoinsAmount(ReinsUtils.addRound(prppDangerCoins.getCoinsAmount(), prppDangerCoins.getChgCoinsAmount(), 2));
                feoCoinsDto.setCoinsPremium(ReinsUtils.addRound(prppDangerCoins.getCoinsPremium(), prppDangerCoins.getChgCoinsPremium(), 2));
                feoCoinsDto.setMiddleCostFee(prppDangerCoins.getMiddleCostFee());
                feoCoinsDtoList.add(feoCoinsDto);
            }
            enquiryDto.setFeoCoinsList(feoCoinsDtoList);
        }
    }

    /**
     * 根据原保单或上次批单修改本次批单的询价单信息主表
     *
     * @param prppDangerUnit 批单危险单位
     * @param reinsEndorUnit 批单接口表
     * @param feoEnquiry     批单临分询价主信息表
     */
    private void genCopyFeoEnquiry(PrppDangerUnit prppDangerUnit, ReinsEndorUnit reinsEndorUnit, FeoEnquiry feoEnquiry) {
        // 批改保单再保确认状态默认为1
        feoEnquiry.setVerifyFlag("2");
        feoEnquiry.setEnquiryNo("Q" + prppDangerUnit.getEndorseNo() + prppDangerUnit.getDangerNo());
        feoEnquiry.setEndorseNo(prppDangerUnit.getEndorseNo());
        feoEnquiry.setInsuredName(reinsEndorUnit.getInsuredName());
        feoEnquiry.setInsuredAddress(reinsEndorUnit.getInsuredAddress());

        feoEnquiry.setEnquiryType(reinsEndorUnit.getCertiType());
        feoEnquiry.setStartDate(prppDangerUnit.getStartDate());
        feoEnquiry.setEndDate(prppDangerUnit.getEndDate());
        feoEnquiry.setCurrency(prppDangerUnit.getCurrency());
        feoEnquiry.setSumAmount(ReinsUtils.addRound(prppDangerUnit.getAmount(), prppDangerUnit.getChgAmount(), 2));
        feoEnquiry.setSumPremium(ReinsUtils.addRound(prppDangerUnit.getPremium(), prppDangerUnit.getChgPremium(), 2));
        if (feoEnquiry.getSumAmount().doubleValue() != 0) {
            feoEnquiry.setRate(feoEnquiry.getSumPremium().divide(feoEnquiry.getSumAmount(), 9, BigDecimal.ROUND_HALF_UP).
                    multiply(new BigDecimal("1000")).setScale(4, BigDecimal.ROUND_HALF_UP));
        } else {
            feoEnquiry.setRate(new BigDecimal("0.00"));
        }
        feoEnquiry.setCreateDate(LocalDate.now());
    }

    /**
     * 删除原有批单询价单信息
     *
     * @param endorNo 批单号
     */
    private void deleteFeoEnquiryEndorse(String endorNo) {
        QueryWrapper<FeoEnquiry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ENDORSENO", endorNo);

        List<FeoEnquiry> feoEnquiryList = iFeoEnquiryService.list(queryWrapper);
        if (feoEnquiryList != null && feoEnquiryList.size() > 0) {
            for (FeoEnquiry feoEnquiry : feoEnquiryList) {
                Map<String, Object> columnMap = new HashMap<String, Object>(16) {{
                    put("ENQUIRYNO", feoEnquiry.getEnquiryNo());
                }};
                iFeoCoinsService.removeByMap(columnMap);
                iFeoEngageService.removeByMap(columnMap);
                iFeoItemService.removeByMap(columnMap);
                iFeoReinsVerifyService.removeByMap(columnMap);
                iFeoTotService.removeByMap(columnMap);
                iFeoReinsReceiveService.removeByMap(columnMap);
                iFeoEnquiryService.removeByMap(columnMap);
            }
        }
    }
}
